1

Parse API response that contains multipart/form-data

I've tried the following, but it's not working. Looking for recommendations

API Response:

    ----1560435280902
    Content-Type: application/json

    {"code":"200","transactionTimestamp":1560435280902,"data":{"imagedDocument": 
    [{"proNumber":"951182326","imageType":"BL","imageFormat":"PDF","imageFiles": 
 [{"base64Data":"Jg==","fileName":"951182326_BL.pdf","contentType":"application/pdf"}],"scanDate":1559312081112}]}}
----1560435280902--

Code that i've tried

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim client = New RestClient("https://api.ltl.xpo.com/imaging/1.0/shipments/951182326/imaged-docs?imageFormat=PDF&multiPartResp=False&imageType=BL")
        Dim request = New RestRequest(Method.[GET])
        request.AddHeader("cache-control", "no-cache")
        request.AddHeader("Connection", "keep-alive")
        request.AddHeader("accept-encoding", "gzip, deflate")
        request.AddHeader("Host", "api.ltl.xpo.com")
        request.AddHeader("Cache-Control", "no-cache")
        request.AddHeader("Accept", "*/*")
        request.AddHeader("Authorization", "Bearer " + accessToken)
        Dim response As IRestResponse = client.Execute(request)

        Dim token As XPOImage = Newtonsoft.Json.JsonConvert.DeserializeObject(Of XPOImage)(response.Content)



Public Class ImageFile
Public Property base64Data As String
Public Property fileName As String
Public Property contentType As String
End Class

Public Class ImagedDocument
Public Property proNumber As String
Public Property imageType As String
Public Property imageFormat As String
Public Property imageFiles As ImageFile()
Public Property scanDate As Long
End Class

Public Class Data
    Public Property imagedDocument As ImagedDocument()
End Class

Public Class XPOImage
    Public Property code As String
    Public Property transactionTimestamp As Long
    Public Property data As Data
End Class

Need to grab the base64 encoded image.

Teddy Higgins
  • 140
  • 10
  • When I take the raw JSON and put it in a file and then use your serialization code off of the file instead of response.Content it works. Maybe use response.Data if that is available (https://stackoverflow.com/a/17172209/341762). Can you please post what response.Content returns? – NoAlias Jun 13 '19 at 15:11
  • the response.Content returns what i put in the above as API Response – Teddy Higgins Jun 13 '19 at 15:13
  • What about regex for grabbing the JSON? – Teddy Higgins Jun 13 '19 at 15:14
  • I think if you are using RestSharp you should have a client.Execute(request).Data which should just be the raw JSON instead of having to parse it out. – NoAlias Jun 13 '19 at 15:21
  • You would have to strip everything that isn't JSON (header, footer) if you want to parse it properly. – the_lotus Jun 13 '19 at 15:22
  • Possible duplicate of [Reading file input from a multipart/form-data POST](https://stackoverflow.com/questions/7460088/reading-file-input-from-a-multipart-form-data-post) – NoAlias Jun 13 '19 at 15:27

1 Answers1

0

I was able to use Regex to grab the JSON from the multi-part response.

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    Dim client = New RestClient("https://api.ltl.xpo.com/imaging/1.0/shipments/951182326/imaged-docs?imageFormat=PDF&multiPartResp=False&imageType=BL")
    Dim request = New RestRequest(Method.[GET])
    request.AddHeader("cache-control", "no-cache")
    request.AddHeader("Connection", "keep-alive")
    request.AddHeader("accept-encoding", "gzip, deflate")
    request.AddHeader("Host", "api.ltl.xpo.com")
    request.AddHeader("Cache-Control", "no-cache")
    request.AddHeader("Accept", "*/*")
    request.AddHeader("Authorization", "Bearer " + accessToken)
    Dim response As IRestResponse = client.Execute(request)

    Dim regexResponse = Regex.Match(response.Content, "\{.*\:\{.*\:.*\}\}")

    Dim token As XPOImage = Newtonsoft.Json.JsonConvert.DeserializeObject(Of XPOImage)(regexResponse.ToString())
Teddy Higgins
  • 140
  • 10