cURL POST to rest endpoint cause error at server - rest

I am trying to make a POST request to my locally running rest endpoint as follows:
curl -X POST -d '[{"name":"xyz","cost":"10"}]' http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"
this results in
Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream#3180b68d; line: 1, column: 2]
When I use other tool for eg. RESTClient plugin in Firefox browser I get an expected response for this post body from my rest endpoint.
Well I enabled server side logging of the request at my Jersey rest endpoint:
The RESTClient request results in:
15:48:31,916 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server has received a request on thread http--127.0.0.1-8080-1
1 > POST http://localhost:8080/xyz/rest/abc
1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
1 > accept-encoding: gzip, deflate
1 > accept-language: null
1 > cache-control: no-cache
1 > connection: keep-alive
1 > content-length: 91
1 > content-type: application/json; charset=UTF-8
1 > host: localhost:8080
1 > pragma: no-cache
1 > user-agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Fire
fox/40.0
15:48:32,086 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server responded with a response on thread http--127.0.0.1-8080-1
1 < 201
1 < Content-Type: application/json
The cURL request results in:
15:49:04,786 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server has received a request on thread http--127.0.0.1-8080-2
2 > POST http://localhost:8080/xyz/rest/abc
2 > accept: application/json
2 > content-length: 76
2 > content-type: application/json
2 > host: localhost:8080
2 > user-agent: curl/7.33.0
15:49:04,792 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server responded with a response on thread http--127.0.0.1-8080-2
2 < 400
2 < Content-Type: text/plain
Now, I know my rest endpoint works correcty, Just curious as to why this is happening for cURL request. Any suggestions are welcome.

OK so the problem is that I am using a windows machine and so single quote around the data is causing this issue.
To get it to work i had to use double quotes around the data and escape the double quotes within the data.
curl -X POST -d "[{\"name\":\"xyz\",\"cost\":\"10\"}]" http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"

Related

How to create file with upload with the help of Tus-Extension header

I wanted to make a request using curl POST, that should create file in my site.
curl -ks -u admin:admin -XPOST https://host.docker.internal:9200/....
-H "Tus-Resumable: 1.0.0"
-H "Upload-Length: 100"
-H "Content-Type: application/offset+octet-stream"
-H "Upload-Metadata: filename dGVzdC50eHQ="
-H "Tus-Extension: creation-with-upload"
-d "upload content"
Documentation of tus says -H "Tus-Extension: creation-with-upload" should create file in site but create file is not visible in my site.
Response
upload completely sent off: 14 out of 14 bytes
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
{ [130 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 201 Created
< Access-Control-Allow-Headers: Tus-Resumable, Upload-Length, Upload-Metadata, If-Match
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: Tus-Resumable, Location
< Content-Length: 0
< Content-Security-Policy: default-src 'none';
< Date: Tue, 13 Dec 2022 06:18:26 GMT
< Location: https://host.docker/data/{randompattern}
< Tus-Extension: creation,creation-with-upload,checksum,expiration
< Tus-Resumable: 1.0.0
< Upload-Expires: 1670998706
< Upload-Offset: 14
< X-Content-Type-Options: nosniff
< X-Download-Options: noopen
< X-Frame-Options: SAMEORIGIN
< X-Permitted-Cross-Domain-Policies: none
< X-Robots-Tag: none
< X-Xss-Protection: 1; mode=block
Currently, no file is created on my site even response is 201 CreatedI expect to create file in my site which i can access through UI?

"Given request parameters are null or invalid" when using curl to submit URLs with IndexNow API

I try to submit URLs to the Bing search engine through IndexNow API using cURL, but I get an "invalid parameter" error, how to fix it?
Here is the PowerShell script
$header_host = '"Host: www.bing.com"'
$header_content = '"Content-Type: application/json; charset=utf-8"'
$url_index =
'"https://www.bing.com/IndexNow"'
$data = '{
"host": "xinlake.dev",
"key": "72d80bb8d9f645ad9e246e46d336c997",
"keyLocation": "https://xinlake.dev/72d80bb8d9f645ad9e246e46d336c997.txt",
"urlList": [
"https://xinlake.dev/blog/2022/05-voting-script/",
"https://xinlake.dev/blog/2022/05-private-channel/"
]
}'
& curl.exe -v --http1.1 --header $header_host --header $header_content --data $data $url_index
""
Here is the error log
* Trying 202.89.233.100:443...
* Connected to www.bing.com (202.89.233.100) port 443 (#0)
* schannel: disabled automatic use of client certificate
* schannel: ALPN, offering http/1.1
* schannel: ALPN, server accepted to use http/1.1
> POST /IndexNow HTTP/1.1
> Host: www.bing.com
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Type: application/json; charset=utf-8
> Content-Length: 278
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Length: 92
< Content-Type: application/json; charset=utf-8
< Expires: -1
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< X-Cache: CONFIG_NOCACHE
< Accept-CH: Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version
< X-MSEdge-Ref: Ref A: C06F23F2577843719494581907366B26 Ref B: BJ1EDGE0917 Ref C: 2022-06-15T10:20:00Z
< Date: Wed, 15 Jun 2022 10:20:00 GMT
<
{"code":"InvalidRequestParameters","message":"Given request parameters are null or invalid"}* Connection #0 to host www.bing.com left intact
Thanks, I have solved this by posting a JSON file.
Script changes
$data = '"path to the json file"'
& curl.exe -v --http1.1 --header $header_host --header $header_content --data #$data $url_index
JSON file
{
"host": "xinlake.dev",
"key": "72d80bb8d9f645ad9e246e46d336c997",
"keyLocation": "https://www.xinlake.dev/72d80bb8d9f645ad9e246e46d336c997.txt",
"urlList": [
"https://www.xinlake.dev/blog/2022/05-voting-script/",
"https://www.xinlake.dev/blog/2022/05-private-channel/"
]
}
Request and response
* Trying 202.89.233.101:443...
* Connected to www.bing.com (202.89.233.101) port 443 (#0)
* schannel: disabled automatic use of client certificate
* schannel: ALPN, offering http/1.1
* schannel: ALPN, server accepted to use http/1.1
> POST /IndexNow HTTP/1.1
> Host: www.bing.com
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Type: application/json; charset=utf-8
> Content-Length: 302
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< X-Cache: CONFIG_NOCACHE
< Accept-CH: Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version
< X-MSEdge-Ref: Ref A: C9107410449C4D1A837A53C5CB1D392C Ref B: BJ1EDGE0416 Ref C: 2022-06-15T14:11:21Z
< Date: Wed, 15 Jun 2022 14:11:21 GMT
< Content-Length: 0
<
* Connection #0 to host www.bing.com left intact

how to split http response in Swift using boundary from Content-Type header param

This is the verbose output using curl to request the server
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 55254 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:55254
> User-Agent: curl/7.64.1
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: WDA MJPEG Server
< Connection: close
< Max-Age: 0
< Expires: 0
< Cache-Control: no-cache, private
< Pragma: no-cache
< Content-Type: multipart/x-mixed-replace; boundary=--BoundaryString
<
I am new to Swift and have some difficulties in finding libraries to read the http response and split it using the boundary param found in Content-Type, I know in golang, we can use the multipart.Reader
multipartReader := multipart.NewReader(resp.Body, boundary)
multipartReader.NextPart()
how do I achieve the same in swift?

How do I tell spray-json on Akka HTTP to "assume application/json"?

Using spray-json on an Akka HTTP server; how can I "assume Content-type: application/json" for requests when not supplied?
Issue
curl localhost:12345/request -d'{"sample":"json"}'
doesn't work unless I add
-H "Content-type: application/json"
explicitly.
Why I ask
not ideal for showing succint examples on a README.md
annoying to add the header manually all the time while debugging
You should have defined you API to accept content-type json only, thats why it does not accept other content-types.
Just doing curl -XPOST -d 'content' URL will post your content as application/x-www-form-urlencoded (default behaviour).
See --verbose of curl POST, see Content-Type in example below
$ curl -XPOST -d '{"test" : "testValue"}' "https://jsonplaceholder.typicode.com/posts" -v
* Trying 104.31.87.157...
* Connected to jsonplaceholder.typicode.com (104.31.87.157) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: sni233425.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
* Server certificate: AddTrust External CA Root
> POST /posts HTTP/1.1
> Host: jsonplaceholder.typicode.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 22
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 201 Created
< Date: Sat, 18 Mar 2017 04:54:41 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 51
< Connection: keep-alive
< Set-Cookie: __cfduid=d91d0ebbaa5f7b40a998d327ffce2b5671489812881; expires=Sun, 18-Mar-18 04:54:41 GMT; path=/; domain=.typicode.com; HttpOnly
< X-Powered-By: Express
< Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
< Access-Control-Allow-Credentials: true
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< X-Content-Type-Options: nosniff
< Etag: W/"33-cwlRczABGtsoiZUP8lw9WSIt684"
< Via: 1.1 vegur
< Server: cloudflare-nginx
< CF-RAY: 3415986bb9f50c35-SEA
<
{
"{\"test\" : \"testValue\"}": "",
"id": 101
* Connection #0 to host jsonplaceholder.typicode.com left intact
}
You have to change your client headers to match what the API server accepts.
$ curl -H "Content-type: application/json" -XPOST -d '{"test" : "testValue"}' "https://jsonplaceholder.typicode.com/posts" -v
* Trying 104.31.87.157...
* Connected to jsonplaceholder.typicode.com (104.31.87.157) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: sni233425.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
* Server certificate: AddTrust External CA Root
> POST /posts HTTP/1.1
> Host: jsonplaceholder.typicode.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-type: application/json
> Content-Length: 22
>
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 201 Created
< Date: Sat, 18 Mar 2017 04:53:22 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 38
< Connection: keep-alive
< Set-Cookie: __cfduid=d8e90de27fc1fa87fb42b763a4199ba7d1489812801; expires=Sun, 18-Mar-18 04:53:21 GMT; path=/; domain=.typicode.com; HttpOnly
< X-Powered-By: Express
< Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
< Access-Control-Allow-Credentials: true
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< X-Content-Type-Options: nosniff
< Etag: W/"26-Zx6VPi+rfbM5YFlBzT2pzGEHcgg"
< Via: 1.1 vegur
< Server: cloudflare-nginx
< CF-RAY: 3415967b2af12a19-SEA
<
{
"test": "testValue",
"id": 101
* Connection #0 to host jsonplaceholder.typicode.com left intact
}
You can change the content-type on you server, and change whatever content you receive to JSON by yourself (which might not a good idea though)

PayPal REST API: trying to activate a billing plan, but getting "HTTP/1.1 401 Unauthorized"

I am building an EC site, where my customers can pay through PayPal API. Now, I am learning how it works, using Sandbox; referring to this page: https://developer.paypal.com/docs/integration/direct/billing-plans/
I have succeeded the prerequisites, i.e., having created a PayPal app,
gotten an access token, and made an API call. And then, I have succeeded making a billing plan, copying-and-pasting the sample command on the page, just replacing the Access Token.
Now, I want to activate the billing plan, but couldn't have succeeded.
The command used is, once again, copied-and-pasted from the page, as below.
curl -v -k -X PATCH https://api.sandbox.paypal.com/v1/payments/billing-plans/P-7DC96732KA7763723UOPKETA/ \
-H 'X-PAYPAL-OAUTH-CONTEXT: {"consumer":{"accountNumber":1181198218909172527,"merchantId":"5KW8F2FXKX5HA"},"merchant":{"accountNumber":1659371090107732880,"merchantId":"2J6QB8YJQSJRJ"},"apiCaller":{"clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP","appId":"APP-6DV794347V142302B","payerId":"2J6QB8YJQSJRJ","accountNumber":"1659371090107732880"},"scopes":["https://api.paypal.com/v1/payments/.*","https://uri.paypal.com/services/payments/futurepayments","openid"]}' \
-H 'Content-Type: application/json' \
-d '[{
"op": "replace",
"path": "/",
"value": {
"state": "ACTIVE"
}
}]'
First, as instructed, I just replaced the Plan ID "P-7DC96732KA7763723UOPKETA" with mine, provided in the previous response for creating the plan.
* Trying 173.0.82.78...
* Connected to api.sandbox.paypal.com (173.0.82.78) port 443 (#0)
* TLS 1.2 connection using TLS_RSA_WITH_AES_256_CBC_SHA256
* Server certificate: api.sandbox.paypal.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> PATCH /v1/payments/billing-plans/P-8AX21799EN516221GJMBOECA/ HTTP/1.1
> Host: api.sandbox.paypal.com
> User-Agent: curl/7.43.0
> Accept: */*
> X-PAYPAL-OAUTH-CONTEXT: {"consumer":{"accountNumber":1181198218909172527,"merchantId":"5KW8F2FXKX5HA"},"merchant":{"accountNumber":1659371090107732880,"merchantId":"2J6QB8YJQSJRJ"},"apiCaller":{"clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP","appId":"APP-6DV794347V142302B","payerId":"2J6QB8YJQSJRJ","accountNumber":"1659371090107732880"},"scopes":["ttps://api.paypal.com/v1/payments/.*","ttps://uri.paypal.com/services/payments/futurepayments","openid"]}
> Content-Type: application/json
> Content-Length: 78
>
* upload completely sent off: 78 out of 78 bytes
< HTTP/1.1 401 Unauthorized
< Date: Fri, 17 Feb 2017 12:18:47 GMT
< Server: Apache
< paypal-debug-id: c46e304b4bb46
< Paypal-Debug-Id: c46e304b4bb46
< Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Dplatformapiserv%26TIME%3D669689432%26HTTP_X_PP_AZ_LOCATOR%3D; Expires=Fri, 17 Feb 2017 12:48:47 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
< Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
< Content-Length: 0
< Connection: close
< Content-Type: text/plain; charset=ISO-8859-1
<
* Closing connection 0
And I tried with some other parameter replacements, like the Client ID, but no luck.
Do I need to replace other parameters?
Or, have I done something wrong?
SOLVED.
On this page, I found simpler and more straightforward command
and made up below:
curl -v -X PATCH https://api.sandbox.paypal.com/v1/payments/billing-plans/<Plan ID>/ \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <Access Token>" \
-d '[
{
"path":"/",
"value":{"state":"ACTIVE"},
"op":"replace"}
]'
Got the response as below:
* Trying 173.0.82.78...
* Connected to api.sandbox.paypal.com (173.0.82.78) port 443 (#0)
* TLS 1.2 connection using TLS_RSA_WITH_AES_256_CBC_SHA256
* Server certificate: api.sandbox.paypal.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> GET /v1/payments/billing-plans/<Plan ID> HTTP/1.1
> Host: api.sandbox.paypal.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type:application/json
> Authorization: Bearer <Access Token>
>
< HTTP/1.1 200 OK
< Date: Sun, 19 Feb 2017 11:36:50 GMT
< Server: Apache
< paypal-debug-id: 3ca31778e271e
< Content-Language: *
< Paypal-Debug-Id: 3ca31778e271e
< Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Dplatformapiserv%26TIME%3D1384360280%26HTTP_X_PP_AZ_LOCATOR%3D; Expires=Sun, 19 Feb 2017 12:06:52 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
< Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
< Vary: Authorization
< Content-Length: 1391
< Connection: close
< Content-Type: application/json
<
* Closing connection 0
{"id":"<Plan ID>","state":"ACTIVE","name":"Sample Plan",
...
"rel":"self","method":"GET"}]}
Got HTTP 200 and the state "ACTIVE".
Thanks for your trying to help, many thanks!