Multipar/related Request in golang

44 views
Skip to first unread message

Afriyie Abraham Kwabena

unread,
Jun 17, 2024, 1:30:03 PM (13 days ago) Jun 17
to golang-nuts
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
}
Reply all
Reply to author
Forward
0 new messages