cURL and Error 411 (Content Length) - json

I'm using cURL to connect to a remote host. Some of my commands execute just fine (like authentication via POST and pulling information via GET). I'm getting a weird error when trying to use a POST to add information with regards to content length. This is my syntax;
curl -k -i -b sessionid -X POST https://hostname/BeyondTrust/api/public/v3/Workgroups/apiTest/Assets?AssetName=string&DnsName=string&DomainName=string -H "Content-Type: application/json" -H "Authorization: PS-Auth key=b8e1...2c27; runas=userName; pwd=[password]" -d ""
This is my output;
[1] 58164
[2] 58165
-bash: -H: command not found
[2]+ Done DnsName=string
HTTP/1.1 411 Length Required
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 27 Oct 2017 14:29:12 GMT
Connection: close
Content-Length: 344
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>
I've researched it, and this is a common problem, apparently. -d "" does not fix and have tried --ignore-content-length and Content-Length: 0 in the headers, but I still have the same issue. Can someone point me in the right direction?

You need to quote the URL because it contains some special shell characters.
This output tells you something went wrong:
[1] 58164
[2] 58165
-bash: -H: command not found
[2]+ Done DnsName=string
Your command got split into multiple jobs and resulted in some errors because of the & in the URL's query string.
Putting the URL in double-quotes " will fix the issue.
curl -k -i -b sessionid \
-X POST \
"https://hostname/BeyondTrust/api/public/v3/Workgroups/apiTest/Assets?AssetName=string&DnsName=string&DomainName=string"
This way your URL will be interpreted properly and the shell won't try to start it in the background.

Related

curl issue: How to post json format data to server using curl in windows cmd?

After posting json format data to server using curl in windows command prompt(cmd), I'm testing the response data now, got a 'Bad request' message.
I've checked the request format, too.
cmd:
curl -i -X POST -H "Content-Type: application/json" -d '{ "isbn":"123442123, 97885654453443","title":"Learn how to build modern web application with MEAN stack","author": "Didin J.","description":"The comprehensive step by step tutorial on how to build MEAN (MongoDB, Express.js, Angular 5 and Node.js) stack web application from scratch","published_year":"2017","publisher":"Djamware.com" }' localhost:3000/api
And then,
HTTP/1.1 400 Bad Request
X-Powered-By: Express
Content-Type: text/plain; charset=utf-8
Content-Length: 11
ETag: W/"b-EFiDB1U+dmqzx9Mo2UjcZ1SJPO8"
Date: Wed, 05 Sep 2018 01:41:55 GMT
Connection: keep-alive
Bad Request
What's wrong? please help me.
You wrote the title as 'curl issue...', but i think curl has never any issue, because it is very widely used.
And your request data have no fault.
As my experience, problem would be exist in DB connection.
please check and check the accuracy of MongoDB connection.

Could not resolve host: dev-fiwr-svc-01.tid.es

Trying to upload some measurements to COSMOS. I have an COSMOS account.
Also, I'm using the tutorial here: https://github.com/ging/fiware-cosmos/blob/master/doc/manuals/quick_start_guide_new.md
I'm stuck on Step 3: Upload some data to HDFS.
This is the process flow for me:
curl -v -X PUT -T measurements.txt "http://storage.dit.upm.es:14000/webhdfs/v1/user/MY-USERNAME/MY-FOLDER/measurements.txt?op=CREATE&user.name=MY-USERNAME" -H "Content-Type: application/octet-stream" -H "X-Auth-token: MY-ACCESS-TOKEN"
After this request, I get the expected response according to the tutorial. But then on the next request...
curl -v -X PUT -T measurements.txt "http://dev-fiwr-svc-01.tid.es:14000/webhdfs/v1/user/MY-USERNAME/MY-FOLDER/measurements.txt?op=CREATE&user.name=MY-USERNAME&data=true" -H "Content-Type: application/octet-stream" -H "X-Auth-token: MY-ACCESS-TOKEN"
...I get the following response:
* Hostname was NOT found in DNS cache
* Could not resolve host: dev-fiwr-svc-01.tid.es
* Closing connection 0
curl: (6) Could not resolve host: dev-fiwr-svc-01.tid.es
Anybody else that has had the same problem?
Thanks to #Shihe Zhang for the idea!
Changed dev-fiwr-svc-01.tid.es:14000 to storage.dit.upm.es:14000 and my file was uploaded.

Special Characters ÆØÅ not received correct with curl

I am sending from curl to a service with REST API and I don’t understand why the server is not receiving special characters ÆØÅ correct.
When I am doing it from curl in CMD, the server is receiving the Danish characters ÆØÅ incorrect.
When I am using the browser, through StrongLoop API, the characters are correctly inserted.
As far as I have been able to research, I need the charset defined. But this have no effect. Using chcp 65001 and Lucida Console font does not help. I am a bit lost identifying the problem.
This is the command used:
curl -X POST -H "Content-Type: application/json; charset=UTF-8" -H "Accept: application/json" -d "{ \"metadata\": { \"user\": \"aa\", \"version\": \"ED\", \"transaction_date\": \"2016-12-30\", \"context_info\": \"string\" }, \"container\": { \"type\": \"SC\", \"type_source\": \"string\", \"op_status_source\": \"string\", \"identification\": \"øæå\", \"id_source\": \"876332d2321\", \"ver\": 0 }}" "http://xx.com/xx/containers?access_token=xxx
The response:
{"type":"SC","type_source":"string","op_status_source":"string","identification":"���","id_source":"8763wwd32d2321","ver":1}
æøå will either be ∩┐╜∩┐╜∩┐╜", ���, or similar, depending on the chcp.
Also note, the console will display æøå correctly, but not the response.
I managed to get this to work by putting the json with Danish characters in its own file, called test.json. I made sure - and this was they key - that the json file was saved as utf-8: I did this by opening the file with notepad and used "Save As", picking utf-8 encoding as file encoding.
At the top of the bat file containing the curl command, I put chcp 65001.
curl.exe -X PUT --header "Content-Type: application/json; charset=UTF-8" --header "Accept: application/json" -s -d #test.json "<theUrl>"
I added -s to make curl go quiet about an error I was getting (Failed writing body), although the command (which updates a record in a database) appears to run successfully.

Sending json files in curl requests with absolute or relative paths

Just wondering how I can send a curl command with the -d option specifying a file with its path and not a file in the current directory.
This is what I'm getting when I try to test my app with the json file in the local dir. Both the app and myself are happy:
curl -XPOST -H 'Content-Type:application/json' -d #all_fields.json http://testcomp.lab.net:8080/stats -v -s
* About to connect() to testcomp.lab.net port 8080
* Trying 10.93.2.197... connected
* Connected to testcomp.lab.net (10.93.2.197) port 8080
> POST /stats HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: testcomp.lab.net:8080
> Accept: */*
> Content-Type:application/json
> Content-Length: 2882
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
HTTP/1.1 200 OK
< Content-Length: 0
* Connection #0 to host testcomp.lab.net left intact
* Closing connection #0
This is what I'm getting when I specify a json file that's in another directory
curl -XPOST -H 'Content-Type:application/json' -d #json/all_fields.json http://testcomp.lab.net:8080/stats -v -s
"Invalid json for Java type interface java.util.List"
Warning: Couldn't read data from file "json/all_fields.json", this makes an
Warning: empty POST.
<snip snip>
<snip snip>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Transfer-Encoding: chunked
* Connection #0 to host testcomp.lab.net left intact
* Closing connection #0
I didn't see anything in the man page for curl for specifying directories for files passed in as data. Am I unfortunately limited to files in the local directory or is there a special way to specify files in different directories? Thanks in advance for your help.
The -d # command option accepts any resolvable file path, as long as the path actually exists. So you could use:
a path relative to the current directory
a fully qualified path
a path with soft-links in it
and so on
To wit, just the same as hundreds of other *Nix style commands. One quick note, the -d option will attempt to url encode your data, which from what you describe isn't actually what you want. You should use the --data-binary option instead. Something like this:
curl -XPOST
-H 'Content-Type:application/json'
-H 'Accept: application/json'
--data-binary #/full/path/to/test.json
http://localhost:8080/easy/eservices/echo -v -s

How to escape characters in part of a JSON POST request with CURL?

So, I'm trying to use a service called Postmark to send a formatted HTML email. They have some API documentation here, and they give this example on how to use CURL:
$: curl -X POST "http://api.postmarkapp.com/email" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Postmark-Server-Token: ed742D75-5a45-49b6-a0a1-5b9ec3dc9e5d" \
-v \
-d "{From: 'sender#example.com', To: 'receiver#example.com', Subject: 'Postmark test', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"
So, that's all good and works just fine when I use my own token. The problem is when I add my own HtmlBody. If I send a simple message, it works just fine. As soon as I add certain special characters, it breaks. For example, if I do something like this:
-d "{From: 'sender#example.com', To: 'receiver#example.com', Subject: 'Postmark test', HtmlBody: '<!DOCTYPE html><html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"
It breaks because of the !. How can I fix this?
UPDATE: As sourcejedi pointed out I am running this from the shell (bash), so the ! issue makes sense to me now. I moved the JSON string to a separate file called email.json and loaded that using -d #email.json. That worked for a simple email with <!DOCTYPE>, but I'm still getting the following error when I try to load the full HTML:
{"ErrorCode":402,"Message":"Received invalid JSON input."}
I believe this has to do with some other special characters. I get the same error when I use --data-urlencode #email.json.
You need to use --data-urlencode, so it should be like curl -X POST --data-urlencode
When I tested on my end..
* About to connect() to api.postmarkapp.com port 80 (#0)
* Trying 50.56.54.211... connected
> POST /email HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1
.2.5
> Host: api.postmarkapp.com
> Accept: application/json
> Content-Type: application/json
> X-Postmark-Server-Token: ed742D75-5a45-49b6-a0a1-5b9ec3dc9e5d
> Content-Length: 164
Still seems to break on me an just returns: -bash: !DOCTYPE: event not found
That's an error from the bash shell. You would need to escape the ! as \! yourself. But you'd be better off reading from a file, I think. Instead of -d data use -d #datafile. (Or -d #- to read from stdin).
I once received the same error when posting JSON data. what I did was enclosed the data with a single quote and use double quote for a string:
so from:
-d "{From: 'sender#example.com', To: 'receiver#example.com', Subject: 'Postmark test', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"
use:
-d '{From: "sender#example.com", To: "receiver#example.com", Subject: "Postmark test", HtmlBody: "<html><body><strong>Hello</strong> dear Postmark user.</body></html>"}'
hope this helps.