Curl POST request in JSON format - ruby-on-rails

I'd like to send a HTTP POST request in JSON format to a specific URL using Curl in the mac terminal.
How do I specify the HTTP verb POST? What is the difference between -d and -X?
How do I specify that I'm sending my data in JSON format?
Any suggestions on how to test the request itself? I'd like to test and see exactly what JSON data is being sent across before I do my 'live' request. Can I run a Rails server on localhost and send my POST request to localhost? How can I see the JSON data?
Any examples are welcome.
Thanks!

1) If you are using the -d option to upload data, curl with automatically use POST. The -X option is used when you want to specify the method (PUT, DELETE etc) rather than getting curl to choose it for you.
echo "how are you" | curl -vvv -d#- http://localhost:8000
* About to connect() to localhost port 8000 (#0)
* Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5
> Host: localhost:8000
> Accept: */*
> Content-Length: 11
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 11 out of 11 bytes
< HTTP/1.1 200 OK
< Date: Sun, 03 Aug 2014 13:46:44 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
2) You can specify that you are sending your data in json format by using the Content-type header. This header can be added in curl using the -H option.
3) Yes you can setup a webserver(using python, nodejs, rails etc) that can just printout the http body once it receives it.

$ curl -d "param1=value1&param2=value2" http://example.com/posts
$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://example.com/posts
I prefer use https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

Related

wso2 apim docker - http://host-ip:9443/publisher is downloading a binary file instead of showing a login page

I am following https://wso2.com/api-management/install/docker/get-started/ this guide for wso2-apim setup but i cannot see any webpage on these url
- Publisher http://localhost:9443/publisher
- Store http://localhost:9443/store
- Admin console http://localhost:9443/admin
- Carbon console http://localhost:9443/carbon
instead on going to these url it downloads a binary file.
docker run -it -p 8280:8280 -p 8243:8243 -p 9443:9443 --name api-manager wso2/wso2am:2.6.0
As was mentioned , all the urls are wrong the correct ones are:
Publisher - https://localhost:9443/publisher
Store - https://localhost:9443/store
Admin console - https://localhost:9443/admin
Carbon console - https://localhost:9443/carbon
Anyway I can't download a "binary" when I reach the url http://localhost:9443/publisher
I used curl command and get this:
curl -v http://127.0.0.1:9443/publisher
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 9443 (#0)
> GET /publisher HTTP/1.1
> Host: 127.0.0.1:9443
> User-Agent: curl/7.54.0
> Accept: */*
>
* Connection #0 to host 127.0.0.1 left intact
P%
Could you add more information. What browser did you used ?

Device not sending data to LWM2M Agent

I'm having trouble with LWM2M device sending data to the IOT Agent. The device connects to the server successfully.
However, it seems the device isn't sending data to the IOT Agent, with no helpful tip, sending the command:
curl --request POST \
--header 'Content-Type: text/plain' \
--header 'Fiware-Service: workshop' \
--header 'Fiware-ServicePath: /' \
--header 'X-Auth-Token: NULL' \
--data 't|30' \
-v \
'http://localhost:4041/iot/d?k=workshop-devices&i=robot1'
It returns the message:
* About to connect() to localhost port 4041 (#0)
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4041 (#0)
> POST /iot/d?k=workshop-devices&i=robot1 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:4041
> Accept: */*
> Content-Type: text/plain
> Fiware-Service: workshop
> Fiware-ServicePath: /
> X-Auth-Token: NULL
> Content-Length: 4
>
* upload completely sent off: 4 out of 4 bytes
< HTTP/1.1 404 Not Found
< X-Powered-By: Express
< Fiware-Correlator: 32da2092-8032-427a-86f2-19ae81a1ab47
< Content-Security-Policy: default-src 'self'
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< Content-Length: 145
< Date: Mon, 24 Sep 2018 10:30:25 GMT
< Connection: keep-alive
<
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /iot/d</pre>
</body>
</html>
* Connection #0 to host localhost left intact
Not sure if anyone understands what's going here please.
Have you followed the steps of https://github.com/telefonicaid/lightweightm2m-iotagent/blob/master/docs/deviceProvisioning.md?
I ask this because using LWM2M, it is not possible to use curl commands to send observations from devices. You need to use an LWM2M client for that purpose.

What params is devise_auth_token expecting?

Devise_auth_token newbie here. I have a rails 5 api only app. I'm trying to make requests to my api using curl and I have managed to do so successfully except for when I want to sign in a user.
curl -XPOST -H 'Content-Type: application/json' -d '{ "session": { "email":"example2#gmail.com", "password": "password"}}' localhost:3000/auth/sign_in
Not sure what I am doing wrong. As I said, for some reason there is no problem when creating a user. When trying to sign in I get this error
{"errors":["Invalid login credentials. Please try again."]}
Any help is greatly appreciated, thank you.
curl -v -XPOST -H 'Content-Type: application/json' -d '{ "email":"admin#example.com", "password": "password"}' localhost:3000/auth/sign_in
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /auth/sign_in HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:3000
> Accept: */*
> Content-Type: application/json
> Content-Length: 55
>
* upload completely sent off: 55 out of 55 bytes
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< access-token: Bm3FaRpSAjT-EtTrj8Ucww
< token-type: Bearer
< client: 6XCiYTxcEITVRZItRKFa8w
< expiry: 1480750998
< uid: admin#example.com
< ETag: W/"be9091473a08ccf2672b9685eb25caa2"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 4996fb00-a45c-41e4-b30b-c86bc10b24d5
< X-Runtime: 0.737506
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
{"data":{"id":1,"email":"admin#example.com","provider":"email","uid":"admin#example.com","name":null,"nickname":null,"image":null}}
Use -v for curl - it will give you many clues.
Comment was right - do not place login / password inside session.
Snip from TL;DR that you should read thoroughly:
The authentication headers consists of the following params: access-token, client, expire, uid
The authentication headers required for each request will be available in the response from the previous request.

Why is this refresh token request to auth.docker.io returning a 404?

I am trying to use the api documented here: https://docker.github.io/registry/spec/auth/oauth/
When I POST to the service as described in the docs, it always returns a 404.
I've tried this using my own account and also using the exact query in the documentation.
Here is an example:
[prompt]$ curl -v \
-H 'Content-Type: application/x-www-form-urlencoded' \
-X POST -d 'grant_type=password&username=johndoe&password=A3ddj3w&service=hub.docker.io&client_id=dockerengine&access_type=offline' \
https://auth.docker.io/token
* Trying 52.5.234.85...
* Connected to auth.docker.io (52.5.234.85) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: *.docker.io
* Server certificate: RapidSSL SHA256 CA - G3
* Server certificate: GeoTrust Global CA
POST /token HTTP/1.1
Host: auth.docker.io
User-Agent: curl/7.43.0
Accept: /
Content-Type: application/x-www-form-urlencoded
Content-Length: 118
upload completely sent off: 118 out of 118 bytes
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Fri, 04 Nov 2016 01:10:14 GMT
< Content-Length: 19
< Strict-Transport-Security: max-age=31536000
<
404 page not found
Connection #0 to host auth.docker.io left intact
The same response is returned when using credentials for an active account.
It seems that I should at least receive a 401 if the account not authenticated.
Is this service no longer supported?
There is a GET api that I can use to acquire a short-lived bearer token, but I need a refresh token.
That GET api is documented here: https://docs.docker.com/registry/spec/auth/jwt/
I was having the same problem. I'll put the solution first, with extra info later.
DOCKER_HUB_USERNAME=$1
DOCKER_HUB_PASSWORD=$2
DOCKER_REPO=$3
AUTH_64=$(echo -n $DOCKER_HUB_USERNAME:$DOCKER_HUB_PASSWORD | base64)
TOKEN=$(curl -s -H "Authorization: Basic $AUTH_64" \
-H 'Accept: application/json' \
"https://auth.docker.io/token?service=registry.docker.io&scope=repository:$DOCKER_REPO:pull" | jq -r .token)
curl -s -H "Authorization: Bearer $TOKEN" -H "Accept: application/json" \
"https://index.docker.io/v2/$DOCKER_REPO/tags/list" | jq -r .tags
I did some digging and found this answer which required you to use docker login, which isn't what I wanted. After looking into the docker conf file he was pulling from, I figured out the value was the base64 encoding of user:pass. So I just avoid trying to get a refresh token.
I used jq to get the json values. Here is a download link. You can retrieve them however you want. I placed the second command in as an example, although this should work with anything from the v2 API. I think you can even replace index.docker.io with any docker registry.
Oh, if you need push access be sure to change $DOCKER_REPO:pull to $DOCKER_REPO:pull,push

Docker Remote API - Pull/Create image not working

Okay so I have enabled managing the docker daemon over HTTP by starting the daemon as follows:
/usr/bin/docker -d -H fd:// -H=0.0.0.0:2376
I can create containers and remove them via the Remote API (i.e other calls are working fine) but if I try and pull an image it errors as follows:
curl -v -X POST http://localhost:2376/images/create?from=ubuntu
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 2376 (#0)
> POST /images/create?from=ubuntu HTTP/1.1
> User-Agent: curl/7.38.0
> Host: localhost:2376
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 01 Oct 2015 09:01:02 GMT
< Transfer-Encoding: chunked
<
{"status":"Downloading from http://"}
{"errorDetail":{"message":"Get http://: http: no Host in request URL"},"error":"Get http://: http: no Host in request URL"}
* Connection #0 to host localhost left intact
Anyone know what the answer is?
Ah looks like it was a typo in the parameter name
"from" -> "fromImage"
Basically you get this error if the query parameters are missing.
Also make sure you set tag=latest otherwise it downloads all ubuntu images!

Resources