Linux curl post request and Error 417 but sudo success? - linux

My service is deployed in docker,the exposed nginx port is 18082;service port is 38087,and I tried both.
while I use command
curl -i -X POST -H 'content-type: text/json' -d #post.json \
http://127.0.0.1:18082/youtu/openliveapi/livedetectfour
return 417
HTTP/1.1 417 Expectation failed
Server: squid/2.7.STABLE9
Date: Tue, 15 Aug 2017 11:57:04 GMT
Content-Type: text/html
Content-Length: 1436
X-Squid-Error: ERR_INVALID_REQ 0
X-Cache: MISS from SK-SQUIDDEV-118
X-Cache-Lookup: NONE from SK-SQUIDDEV-118:8080
Connection: close
But when I add sudo in the front,it return success.
HTTP/1.1 100 Continue
HTTP/1.1 200 OK Server: openresty/1.9.15.1
I really do search and knew while curl post over 1024 bytes,it will first send
expect 100-continue.if server don't support that request, it will return 417 error.
then how can sudo succsess,maybe It is related to nginx mechanism,I'm not familiar with that.thanks.

Since you are using proxy in your corporate setup and the proxy might be insert in your user's bash profile. You can do 3 things
Update ~/.bash_profile or ~/.bashrc
Remove the proxy from your profile and you won't need the proxy
Unset the variables
You can unset the variables before the call
unset http_proxy
unset https_proxy
curl -i -X POST -H 'content-type: text/json' -d #post.json \ http://127.0.0.1:18082/youtu/openliveapi/livedetectfour
Blank the variables for the call
You can set the variables just for that one curl call
http_proxy= https_proxy= curl -i -X POST -H 'content-type: text/json' -d #post.json \ http://127.0.0.1:18082/youtu/openliveapi/livedetectfour

Related

Evaluate parameters in curl API call

I got a curl that had to be rewritten recently, looking like this:
cmd=$("curl https://exampleurl/query -sX POST -H Authorization: Bearer $ingestToken -H Content-Type: application/json -H Accept: application/json -H Accept-Charset: utf-8 -d {\"queryString\":\"$query\", \"start\": \"$start_time\", \"end\": \"$stop_time
This returns the error "No such file or directory"
I got a working version using eval $cmd, and I have compared the two curl strings generated by each method, and they look the same. Any ideas what is causing the error in this case?

Eclipse Hono - 401 Unauthorized Error (Even when the credentials are correct)

I am using the following commands to create a tenant in Eclipse Hono
$ curl -X POST -i -H 'Content-Type: application/json' -d '{"tenant-id": "testenant1"}'
http://localhost:28080/tenant
HTTP/1.1 201 Created
location: /tenant/testenant1
content-length: 0
Registering a device in the tenant using the below command
curl -X POST -i -H 'Content-Type: application/json' -d '{"device-id": "1"}'
http://localhost:28080/registration/testenant1
HTTP/1.1 201 Created
location: /registration/testenant1/1
content-length: 0
Authenticating the registered device using the below command
$ curl -i -X POST -H 'Content-Type: application/json' --data-binary '{
"device-id": "1",
"type": "hashed-password",
"auth-id": "newAuth1",
"secrets": [{
"pwd-plain": "mylittle"
}]
}' http://localhost:28080/credentials/testenant1
HTTP/1.1 201 Created
location: /credentials/testenant1/newAuth1/hashed-password
content-length: 0
When I try to send data to this registered and Authenticated device using the below command.
curl -X POST -i -u newAuth1#testenant1:mylittle -H 'Content-Type: application/json' -d '{"temp": 23.07, "hum": 45.85}' http://localhost:8080/telemetry
HTTP/1.1 401 Unauthorized
content-length: 0
I will be getting 401 Unauthorized error (I am expecting 503 - Service Unavailable error).
Note: I was using the similar approach before and it was working perfectly fine. I am not sure if I am missing something.
You are using wrong credentials when POSTing the data. The username always consists of the auth-id and the tenant-id separated by #.
You need to use:
curl -X POST -i -u newAuth1#testenant1:mylittle -H 'Content-Type: application/json' -d '{"temp": 23.07, "hum": 45.85}' http://localhost:8080/telemetry
That said, based on the URIs you are using for registering the tenant and device, you seem to be using quite an old version of Hono. Please consider upgrading to the latest version (1.1.1) in order to take advantage of recent development/bug fixing ...

Convert curl request into http request?

I am trying to convert the curl request below into an HTTP request for the postman tool. The postman tool might not really matter in this question. Please tell me how I can convert curl to http.
curl -X POST -i 'https://a-webservice.com' -H X-apiKey:jamesBond007 -d MESSAGE-TYPE="pub.controller.user.created" -d PAYLOAD='a json object goes here!'
What I tried/learned:
- Set headers content-type: json/application, X-apiKey
from curl docs, -d option means we need to set content-type application/x-www-form-urlencoded
Postman lets me set the request body using ONLY 1 of the 4 options- form-data, x-www-form-urlencoded, raw, binary. Can you show how I can convert the two -d options of curl into these options ?
I am confused how to put it all together.
Thanks!
The format of application/x-www-form-urlencoded data is just the same as a query string, so:
MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!
To confirm, you can dump the request data with curl itself, using the --trace-ascii option:
curl --trace-ascii - -X POST -i 'https://a-webservice.com' \
-H X-apiKey:jamesBond007 -d MESSAGE-TYPE="pub.controller.user.created" \
-d PAYLOAD='a json object goes here!'
--trace-ascii takes a filename as an argument but if you give it - it will dump to stdout.
The output for the above invocation will include something like this:
=> Send header, 168 bytes (0xa8)
0000: POST / HTTP/1.1
0011: Host: example.com
0024: User-Agent: curl/7.51.0
003d: Accept: */*
004a: X-apiKey:jamesBond007
0061: Content-Length: 73
0075: Content-Type: application/x-www-form-urlencoded
00a6:
=> Send data, 73 bytes (0x49)
0000: MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object g
0040: oes here!
== Info: upload completely sent off: 73 out of 73 bytes
So the same as what’s confirmed in the answer at Convert curl request into http request? that uses nc, but confirmed just using curl itself, with the --trace-ascii option.
I don't know much about Postman. But I captured what's being sent in a file named /tmp/ncout. Based on this, we see that the Content-Type being sent is application/x-www-form-urlencoded, as you determined, and that the payload being sent is MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!.
Does that help?
alewin#gobo ~ $ nc -l 8888 >/tmp/ncout 2>&1 </dev/null &
[1] 15259
alewin#gobo ~ $ curl -X POST -i 'http://localhost:8888/' -H X-apiKey:jamesBond007 -d MESSAGE-TYPE="pub.controller.user.created" -d PAYLOAD='a json object goes here!'
curl: (52) Empty reply from server
[1]+ Done nc -l 8888 > /tmp/ncout 2>&1 < /dev/null
alewin#gobo ~ $ cat /tmp/ncout
POST / HTTP/1.1
Host: localhost:8888
User-Agent: curl/7.43.0
Accept: */*
X-apiKey:jamesBond007
Content-Length: 73
Content-Type: application/x-www-form-urlencoded
MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!alewin#gobo ~ $
Here's an example of how to do this urlencode with python:
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
> data = {'MESSAGE-TYPE': "pub.controller.user.created", 'PAYLOAD': 'a json object goes here!'}
> from urllib import urlencode
> urlencode(data)
PAYLOAD=a+json+object+goes+here%21&MESSAGE-TYPE=pub.controller.user.created

Curl --data command usage

I am using the following command for getting the website data,
curl --data '{"xxxNamespace": "xxxxxx", "h_xxx_action": "xxxxx"}' -i -u username/password -H "Content-Type:application/json" -H "Accept:application/json" -X GET
I would like to download the webpage. But this command results in some html value as o/p,
**HTTP/1.1 200 OK**
Date: Sat, 01 Oct 2016 15:32:19 GMT
Server: Apache/2.2.3 (Red Hat)
Content-ID: -75f83766:15766e71efd:6afa
Set-Cookie: CRN=showWelcomePage%3Dtrue%26automaticPageRefresh%3D30%26displayMode%3Dlist%26useAccessibilityFeatures%3Dfalse%26columnsPerPage%3D3%26linesPerPage%3D15%26timeZoneID%3DWET%26productLocale%3Den%26listViewSeparator%3Dnone%26showOptionSummary%3Dtrue%26showHiddenObjects%3Dfalse%26contentLocale%3Den%26format%3DHTML%26skin%3Dcorporate%26; Domain=.xxx-xx.com; Path=/xx
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html;charset=UTF-8
Kindly help me with the command, Am I doing something wrong ?
I tried with -X GET Option as well, but it returned the same o/p. Kindly help

Trouble replicating CouchDB database to IBM Cloudant

I'm trying to replicate a database from my local CouchDB installation to IBM Cloudant. I used curl for this, like this -
curl -X POST 'http://10.88.201.198:5984/_replicate'
-d '{
"source":"http://10.88.201.198:5984/temperature-db",
"target":"username:password#username.cloudant.com/temperature-db"
}'
-H "Content-Type: application/json"
-v
The attempt times out, and I get the following message -
upload completely sent off: 255 out of 255 bytes
< HTTP/1.1 500 Internal Server Error
* Server CouchDB/1.6.1 (Erlang OTP/R16B03) is not blacklisted
< Server: CouchDB/1.6.1 (Erlang OTP/R16B03)
< Date: Wed, 27 Apr 2016 09:18:34 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 20
< Cache-Control: must-revalidate
< Proxy-Connection: Keep-Alive
< Connection: Keep-Alive
<
{"error":"timeout"}
How do I go about fixing this ?
you need add the user and password of some user who has permission to replicate and on target miss the 'https://' and use localhost or 0.0.0.0
curl -X POST 'http://username:password#localhost:5984/_replicate'
-d '{
"source":"http://username:password#localhost:5984/temperature-db",
"target":"https://username:password#username.cloudant.com/temperature-db"
}'
-H "Content-Type: application/json"
-v
this maybe help
Chances are your replication is taking a while to complete. To get around this you should use the _replicatordatabase and you can monitor that at your leisure. There is documentation on the cloudant website on how to set up a replication using the _replicator database.

Resources