Jump to content

Using REST (CURL) with options - Get works => Post error 401 Unauthorised


Recommended Posts

Hi
I try to communicate with a network recorder. using HTTP Rest api.
Here an Example from the Manual:
curl -k https://192.168.153.193:8443/api/v1/settings/stream/status/1
Answer:
{
"status": "Enable"
}

That also works in LabVIEW which is great so far.


When I try to use the POST I do fail with LabVIEW

Here an example from the Manual using Post:
curl -k --basic -u JohnDoe:123456 -X POST https://192.168.153.193:8443/api/v1/settings/stream/status/1 -d "{\"action\": \"save\",\"data\": {\"status\": \"Enable\"}}"

I tried the above POST command using "putty" - so I know, that it should work according to the manual.
 

Following picture shows the implementation in LabVIEW.

As you see, I get the right ansver for GET but I get "Unauthorised" Response wit Error code 401.

Any Idea how I can solve this?
Screenshot2024-06-28100336.thumb.png.555aa7132b26cc6bd53c9a5014aa91bf.png

Link to comment
Share on other sites

Posted (edited)

Update:
I just saw that above Post Command:
curl -k --basic -u JohnDoe:123456 -X POST https://192.168.153.193:8443/api/v1/settings/stream/status/1 -d "{\"action\": \"save\",\"data\": {\"status\": \"Enable\"}}"

also works in Cmd Window 🙂


So I also tried out the System Exec.vi and that also works.

So basically Problem solved but just with a workaround.

Basically I can do both now using System Exec.vi => GET and POST

 

But I still wonder: Is there an idea how to do it using REST API?

 

Edited by martin_h
Link to comment
Share on other sites

  • 2 weeks later...
Posted (edited)

Hi
Problem solved within LabVIEW (thx to Support from NI).

(Translated from german sorry guys)

When an HTTP request is sent using LabVIEW, the authorization headers are not added to the request, 
so the server rejects the request. LabVIEW should actually add the Authorization headers automatically 
when these fields are used in the Open HTTP Handle.vi file, but the following figure shows that there are no headers.

Therefore, a colleague of mine created a VI that creates the headers manually.

With this VI I can now make POST requests to endpoints that require authentication.

 

As you see from NI Example I need to send the basic Authorisation using the Add Header vi.

Kinda special for me is, that there is/was not yet an entry in the combobox for Authorisation.
The base64 fast encode 2012 NIVerified.vi id used to encode the username:password string.

Following 2 examples (Both work).
First using LabVIEW 2019 HTTP functions. Second using JKI Rest API using LabVIEW 2023.

Someting that was wrong in my case was that I have not removed the back slash in the send data.
"{\"action\": \"save\",\"data\": {\"status\": \"Enable\"}}"  => {"action": "save","data": {"status": "Enable"}}
 

 

image.png.cab21a1e96d2015e3e9151d6985464aa.pngimage.thumb.png.1ffe0d3b94a5aeeb157b670f215f636c.png

Edited by martin_h
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.