LabVIEW Web Development Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP Post picture via JSON

Solved!
Go to solution

Hi to all,

 

Relatively beginner with the HTTP Client library, I was able successfully to send JSON commands via Labview.

Actually I'm working in order to post via Webex pictures.

 

In the official Cisco manual, the cURL command it would be as example:

curl --request POST \

--header "Authorization: Bearer ACCESS_TOKEN" \

--form "roomId=Y2lzY2....." \

--form "text=example attached" \

--form "files=@/home/desktop/example.png;type=image/png" \

https://webexapis.com/v1/messages

 

I am able to create a message, but I have no clue at the end to physically post the file.

Do you have any example to share?

 

I tried with the VI in picture, but answers are:

HTTP/1.1 400 Bad Request
Date: Tue, 01 Aug 2023 19:13:50 GMT
Content-Type: application/json
Connection: close
via: 1.1 linkerd, 1.1 linkerd
trackingid: ROUTER_xxxxx
server: Redacted
x-content-type-options: nosniff
x-content-type-options: nosniff
vary: accept-encoding
strict-transport-security: max-age=63072000; includeSubDomains; preload

 

{"message":"The request could not be understood by the server due to malformed syntax.","errors":[{"description":"The request could not be understood by the server due to malformed syntax."}],"trackingId":"ROUTER_xxxxx"}

 

Thanks so much.

0 Kudos
Message 1 of 4
(2,012 Views)
Solution
Accepted by topic author FrancescoBar

A couple things that look off in your screenshot:

 

  • Don't override the Content-Type header in this case. Your content type is multipart/form-data, not application/json. The POSTMultipart VI will add the right Content-Type for you. It's a bit more complex than normal as the multipart/form-data Content-Type includes a calculated boundary= identifier so let the POSTMultipart VI set it for you.
  • All the Form Data (each of the --form options) should be included in the postdata cluster array not added as headers. They are part of the multipart/form-data body and not the request headers.
  • You could consider using the third party tool https://httpbin.org/post to test the request you make with curl vs LabVIEW HTTP Client to debug and compare the differences between them.

 

formdata.png

Note: In this example I used the name + value + filename to indicate a server side file but in your example you can use name + file + filename. See the POSTMultipart VI docs about postdata cluster configuration.

 

Example output from the third party httpbin tool showing the two form fields and the file were identified as well as the Authorization header:

 

MilanR_0-1690919897806.png

 


Milan
0 Kudos
Message 2 of 4
(1,998 Views)

Hi,

I just created a work-around using the cmd terminal, and it's working.

 

Thanks for the reply.

I will try your suggestion later, to see if I can follow your instructions.

 

0 Kudos
Message 3 of 4
(1,989 Views)

Thanks so much for the support!

I was able to send correctly the file.

Message 4 of 4
(1,971 Views)