Hi,

AM trying to prepare a multipart/related message which involve JSON data 
n1n2Request.JsonData and a binary data binaryData to be sent in HTTP 
request, however, am having issue abou how to set the boundries in the 
function below. The server is logging "Failed to parse boundary. 
missing/invalide boundary". I need help to set the boundaries correctly in 
the function below. Thanks in advance.



func CreateMultipartRelatedN1N2MessageTransferRequest(inputData 
models.InputData) (*models.N1N2MessageTransferRequest, string, error) {
    // Create a buffer to hold the multipart data
    var buf bytes.Buffer

    // Create a multipart writer with "multipart/related" type
    writer := multipart.NewWriter(&buf)

    // Define the boundary for the multipart/related content
    boundary := writer.Boundary()

    // Create the N1N2MessageTransferRequest object
    n1n2Request := models.N1N2MessageTransferRequest{}

    // Get LMF instanceID (example placeholder, replace with actual logic)
    _, _, nfId, _, _ := config.Cfg.GetServerInfo()

    // Populate the N1N2MessageTransferReqData
    n1n2Request.JsonData = &models.N1N2MessageTransferReqData{
        N1MessageContainer: &models.N1MessageContainer{
            N1MessageClass: models.N1MessageClass_LPP,
            N1MessageContent: &models.RefToBinaryData{
                ContentId: "N1LoInformation",
            },
            NfId: nfId,
        },
        LcsCorrelationId:       inputData.CorrelationID,
        N1n2FailureTxfNotifURI: "",
    }

    // Add the JSON part as the root body part
    jsonPartHeader := textproto.MIMEHeader{}
    jsonPartHeader.Set("Content-Type", "application/json; charset=UTF-8")
    jsonPartHeader.Set("Content-ID", "jsondata")
    jsonPart, err := writer.CreatePart(jsonPartHeader)
    if err != nil {
        return nil, "", fmt.Errorf("error creating JSON part: %v", err)
    }

    jsonBytes, err := json.Marshal(n1n2Request.JsonData)
    if err != nil {
        return nil, "", fmt.Errorf("error marshaling JSON data: %v", err)
    }
    _, err = jsonPart.Write(jsonBytes)
    if err != nil {
        return nil, "", fmt.Errorf("error writing JSON data: %v", err)
    }

    // Create the LPP message
    lppMessage := models.LPPMessage{
        TransactionID: &models.LPPTransactionID{
            TransactionNumber: common.GenerateNumber(),
            Initiator:         int(models.LocationServer),
        },
        SequenceNumber: common.GenerateNumber(),
        LPPMessageBody: &models.LPPMessageBody{
            C1: &models.C1{
                RequestLocationInformation: 
&models.RequestLocationInformation{
                    CriticalExtensions: &models.CriticalExtensions{
                        C1: &models.CriticalExtensionsC1{
                            RequestLocationInformationR9: 
&models.RequestLocationInformationR9IEs{
                                CommonIEsRequestLocationInformation: 
&models.CommonIEsRequestLocationInformation{
                                    LocationInformationType: 
int(models.LocationEstimateRequired),
                                    PeriodicalReporting: 
&models.PeriodicalReportingCriteria{
                                        ReportingAmount:   int(models.RA1),
                                        ReportingInterval: int(models.RI32),
                                    },
                                    LocationCoordinateTypes: 
&models.LocationCoordinateTypes{
                                        EllipsoidPoint: true,
                                    },
                                    VelocityTypes: &models.VelocityTypes{
                                        HorizontalVelocity: true,
                                    },
                                },
                            },
                        },
                    },
                },
            },
        },
        EndTransaction: true,
    }

    // Encode the LPP message using ASN.1 PER encoding
    binaryData, err := aper.Marshal(lppMessage)
    if err != nil {
        logger.Log.Error().Msgf("Error encoding, aper.Marshal error: %v", 
err)
        return nil, "", err
    }

    // Add the binary part with a Content-ID header
    binaryPartHeader := textproto.MIMEHeader{}
    binaryPartHeader.Set("Content-ID", "n1message")
    binaryPartHeader.Set("Content-Type", "application/vnd.3gpp.5gnas")
    binaryPart, err := writer.CreatePart(binaryPartHeader)
    if err != nil {
        return nil, "", fmt.Errorf("error creating binary part: %v", err)
    }

    // Write the encoded binary data to the binary part
    _, err = binaryPart.Write(binaryData)
    if err != nil {
        return nil, "", fmt.Errorf("error writing binary data: %v", err)
    }

    // Close the multipart writer to finalize the form data
    err = writer.Close()
    if err != nil {
        return nil, "", fmt.Errorf("error closing multipart writer: %v", 
err)
    }

    // Attach the buffer's content as the binary data of the 
N1N2MessageTransferRequest
    n1n2Request.BinaryDataN1Message = buf.Bytes()

    contentType := fmt.Sprintf("multipart/related; boundary=%s", boundary)

    return &n1n2Request, contentType, nil
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/249033ac-0513-408a-a0e8-21416a2ec820n%40googlegroups.com.

Reply via email to