Curl API request value in body always null - json

I have the following controller I am trying to use Curl to test it. The problem is that fiscalId is always null its not reading my post body from curl.
Controler:
[Route("api/token")]
public class TokenController : Controller
{
[HttpPost("changefiscal")]
[Authorize]
public async Task<ActionResult> ChangeFiscal([FromBody] long fiscalId)
{
//fiscalId is always 0
}
}
I have checked the documentation here which says to use the -d parameter and post it as a json object. Unfortunately thats not working
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
I have tried
curl -d "fiscalId=21875" -H "Content-Type: application/json" -H "Authorization: Bearer XXX" -X POST -i http://localhost:5000/api/token/changefiscal
and
curl -d "{"fiscalId":"21875"}" -H "Content-Type: application/json" -H "Authorization: Bearer XXX" -X POST -i http://localhost:5000/api/token/changefiscal
and
curl -d "{"fiscalId":"21875"}" -H "Content-Type: application/json" -H "Authorization: Bearer XXX" -X POST -i http://localhost:5000/api/token/changefiscal
and
curl -d "{\"fiscalId\":\"21875\"}" -H "Content-Type: application/json" -H "Authorization: Bearer XXX" -X POST -i http://localhost:5000/api/token/changefiscal
and everything else i can think of
I can see in the log that it appears to be sending it
Rebuilt URL to: POST/
timeout on name lookup is not supported
getaddrinfo(3) failed for POST:80
Couldn't resolve host 'POST'
Closing connection 0 curl: (6) Couldn't resolve host 'POST'
timeout on name lookup is not supported
Trying ::1...
TCP_NODELAY set
Trying 127.0.0.1...
TCP_NODELAY set
Connected to localhost (127.0.0.1) port 5000 (#1)
POST /api/token/changefiscal HTTP/1.1
Host: localhost:5000
User-Agent: curl/7.53.1
Accept: /
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjI2OTAzNzc0NjYyNzc4RDM1MzE2QUUwQUU1MjIyQUU1REIzM0M0NUEiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJKcEEzZEdZbmVOTlRGcTRLNVNJcTVkc3p4Rm8ifQ.eyJuYmYiOjE1MTA1NjcxNzMsImV4cCI6MTUxMDU3MDc3MywiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJ0ZXN0YXBpIl0sImNsaWVudF9pZCI6IlNlcnZpY2VBY2NvdW50QWNjZXNzIiwic3ViIjoiMjEyNDg1ODIiLCJhdXRoX3RpbWUiOjE1MTA1NjcxNzMsImlkcCI6ImxvY2FsIiwibmFtZSI6ImxpbGF3IiwiZS1tYWlsIjoibGlsYXdAZWcuZGsiLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiwidGVzdGFwaSJdLCJhbXIiOlsicGFzc3dvcmQiXX0.Q_oJ-xOIKdTtGoRqJbtNjwLV82cQQLhJUVl2Ey-4riZdp45rPIrmfOw9VdknUOLpm8RYZ0iLFC31gCpn1vKb4zLiTmBuTuzYsKxnAvf-UkCj1L8soH4W2lCvx560bZeS7B67tJ4nK9t36OiMLQY4m-_NRTSRBs5QtsXBd5VQGPsTpxf_MxVbQm6Hj6Ot3FGRIa08gmwD3iEr9vsCU6732InbGzU26wtm-WLWp_A_k66Z29G8ms_U8x6gg-aOlQG9_0v_DEjTXCkKNcgWoL0IvaCC0psiYkdIjQxaOwz-e_fcqXpKEFmuoSqmGd1l-eMl3HimOomdOHwqploWGxnJ4Q
Content-Length: 7
upload completely sent off: 7 out of 7 bytes
However when i check fiscalId its always 0 i even tried to change fiscalId to a string and its just null.

When you POST data as JSON object, like -d '{"fiscalId": 21875}', your action method should expect some simple class with corresponding properties. In your case:
public class Dto
{
public long fiscalId {get; set;}
}
and
public async Task<ActionResult> ChangeFiscal([FromBody] Dto data)

Finally after more then an hour of playing with this I got it even though the content-Type is json its just sent as a string "'21875'" even though all the documentation shows sending it as JSon. For some reason the API isnt able to read it as a JSon Object but expects it just to be sent as a string.
curl POST -v -d "'21875'" -H "Content-Type: application/json" -H "Authorization: Bearer XXX" http://localhost:5000/api/token/changefiscal

Related

JSON Unmarshalling error sending metric to Datadog via cURL

I'm trying to send Jenkins build data to Datadog via cURL command but it fails with Invalid JSON structure error. I've validated that the JSON payload I'm sending is valid.
I'm sending metric by run shell in my Jenkins groovy file that runs a curl command.
Groovy:
sh """curl -X POST "${uriDataDog}" -H "Content-type: application/json" -H "DD-API-KEY: ${DD_API_KEY}" -d '${finalPayload}' """
This is executed as:
curl -X POST https://api.datadoghq.com/api/v2/series -H 'Content-type: application/json' -H 'DD-API-KEY: ****' -d '{"series":[{"metric":"jenkins.step.duration","type":0,"points":[{"value":8,"timestamp":1655186740}],"tags":{"stage":"post_always"}}]}'
But it errors out with the following error:
`{"errors":["Invalid JSON structure: json: cannot unmarshal object into Go value of type []json.RawMessage"]}`
Have validated the same with the Datadog documentation and nothing seems wrong.
I've also tried using alternatives to this as
sh(script: "curl -X POST $uriDataDog -H \"Content-type: application/json\" -H \"DD-API-KEY: $DD_API_KEY\" -d '$finalPayload'")
and
["curl", "-X", "POST", "${uriDataDog}", "-H", "DD-API-KEY: ${DD_API_KEY}","-H", "Content-Type: application/json", "-d", "'${finalPayload}'"].execute().text
but even this doesn't work.

Sending JSON Data to Solr using cUrl

How do I can send JSON object to solr collection usinc cUrl
I'm using Windows 10.
curl -X POST -H "Content-Type:application/json" "http://localhost:8983/solr/solr_sample/update/json/docs" --data-binary "{'id': '1','title':'Doc 1'}"
When I'm using this format I'm getting some kind of warning message:
curl -X POST -H 'Content-Type:application/json' 'http://localhost:8983/solr/sorl_sample/update/json/docs' --data-binary '{"id": "1","title":"Doc 1"}'
curl: (1) Protocol "'http" not supported or disabled in libcurl
curl: (3) [globbing] unmatched close brace/bracket in column 14
I resolved it using " " insted of ' '
When I send the request using the first url I'm getting this response:
{
"responseHeader":{
"status":0,
"QTime":112}}
But when I try to get some result by searching I can't see any object in docs[]
curl -X GET "http://localhost:8983/solr/solr_sample/select?q=*:*"
Result:
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*"}},
"response":{"numFound":0,"start":0,"docs":[]
}}
When I'm using Solr UI I can add JSON objects without any problems, also to see the result in terminal
You have to commit the update. Add commit true or commitWithin 1000 to the request, e. g.
curl -X POST -d '{"add":{"doc":{"id":"delete.me","title":"change.me"},"commitWithin":1000}}' -H "Content-Type: application/json" http://localhost:8983/solr/solr_sample/update
Does also work:
curl -X POST -d '{"add":{ "doc":{"id":"delete.me","title":"change.me"}}}' -H "Content-Type: application/json" http://localhost:8983/solr/solr_sample/update?commit=true

Failed conversion from dwg to svf with the Autocad api TranslationWorker

I am failing to upload a simple dwg and convert it to svf using the TranslationWorker from the forge API. I am using autocad 2014 and creating a simple drawing with a circle and saving it as a .dwg.
I am using the commandline in windows and the following curl commands:
curl -v "https://developer.api.autodesk.com/oss/v2/buckets/kumkortbucket/objects/sirkel.dwg" -X "PUT" -H "Authorization: Bearer OAUTH_TOKEN" -H "Content-Type: application/octet-stream" -T "‪C:\Users\DAN\Documents\Testfiler\sirkel.dwg"
which produces the following result:
HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: Authorization, Accept-Encoding, Range,Content-Type
< Access-Control-Allow-Methods: GET
< Access-Control-Allow-Origin: *
< Content-Type: application/json; charset=utf-8
< Date: Tue, 14 Feb 2017 12:56:38 GMT
< Server: Apigee Router
< Content-Length: 355
< Connection: keep-alive
<
{
"bucketKey" : "kumkortbucket",
"objectId" : "urn:adsk.objects:os.object:kumkortbucket/sirkel.dwg",
"objectKey" : "sirkel.dwg",
"sha1" : "78f92dc0a364814756cfa9392d7fa95aecf0916b",
"size" : 31548,
"contentType" : "application/octet-stream",
"location" : "https://developer.api.autodesk.com/oss/v2/buckets/kumkortbucket/objects/sirkel.dwg"
}* Connection #0 to host developer.api.autodesk.com left intact
I then convert it using the POST job command
curl -X "POST" -H "Authorization: Bearer OAUTH_TOKEN" -H "Content-Type: application/json" -v "https://developer.api.autodesk.com/modelderivative/v2/designdata/job" -d "{\"input\": {\"urn\": \"dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6a3Vta29ydGJ1Y2tldC9zaXJrZWwuZHdn\"},\"output\": {\"formats\": [{\"type\": \"svf\",\"views\": [\"2d\",\"3d\"]}]}}"
Using the GET command to check the result i get the following result:
{"type":"manifest","hasThumbnail":"false","status":"failed","progress":"complete","region":"US","urn":"dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6a3Vta29ydGJ1Y2tldC9zaXJrZWwuZHdn","version":"1.0","derivatives":[{"name":"sirkel.dwg","hasThumbnail":"false","status":"failed","progress":"complete","messages":[{"type":"error","code":"AutoCAD-InvalidFile","message":"Sorry, the drawing file is invalid and cannot be viewed. \n- Please try to recover the file in AutoCAD, and upload it again to view."},{"type":"error","message":"Unrecoverable exit code from extractor: -1073741831","code":"TranslationWorker-InternalFailure"}],"outputType":"svf"}]}* Connection #0 to host developer.api.autodesk.com left intact
I cannot figure out what i am doing wrong. I can view the file in autocad and the online viewer.
Cheers.
EDIT:
here is a file that is problematic to upload
http://www.filedropper.com/ekbsroplain
Here is what I did (entire process):
curl -v 'https://developer.api.autodesk.com/authentication/v1/authenticate' -X 'POST' -H 'Content-Type: application/x-www-form-urlencoded' -d 'client_id=****&client_secret=****&grant_type=client_credentials&scope=data:read data:write data:create bucket:create bucket:read'
curl -v 'https://developer.api.autodesk.com/oss/v2/buckets' -X 'POST' -H 'Content-Type: application/json' -H 'Authorization: Bearer TOKEN' -d '{"bucketKey":"kumkortbucket170214","policyKey":"transient"}'
Note I'm uploading without content-type and content-length, not need actually:
curl -v 'https://developer.api.autodesk.com/oss/v2/buckets/kumkortbucket170214/objects/drawing1.dwg' -X 'PUT' -H 'Authorization: Bearer TOKEN' -T '/Users/augustogoncalves/Desktop/Drawing1.dwg'
And finally the POST JOB:
curl -X 'POST' -H 'Authorization: Bearer TOKEN' -H 'Content-Type: application/json' -v 'https://developer.api.autodesk.com/modelderivative/v2/designdata/job' -d '{"input":{"urn":"dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6a3Vta29ydGJ1Y2tldDE3MDIxNC9kcmF3aW5nMS5kd2c="},"output":{"formats":[{"type": "svf","views": ["2d","3d"]}]}}'
And worked fine
{
"result":"success",
"urn":"dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6a3Vta29ydGJ1Y2tldDE3MDIxNC9kcmF3aW5nMS5kd2c",
"acceptedJobs":{
"output":{
"formats":[
{
"type":"svf",
"views":[
"2d",
"3d"
]
}
]
}
}
}

post data using curl command

I have tried posting data in curl using this command:
curl -X POST http://x.x.x.x:1111/send --data [{'virtual':'5555'}]
Then I am getting a response in node like this:
{ '{virtual:5555}': '' }
How to convert this to array? I have tried JSON.parse() and JSON.stringify() but I get an Unexpected token o error as result.
Any idea how to post the json data in curl command?
I also tried with:
curl ... -H "Accept: application/json" -H "Content-Type: application/json"
Try with:
curl -X POST -H "Content-Type: application/json" -d '[{"virtual":"5555"}]' "http://x.x.x.x:1111/send"
Then you should receive the raw body as '[{"virtual":"5555"}]':
var body = JSON.parse(req.body);
or, already parsed (if you use a middleware). And you can use it:
console.log(body.length); // 1
console.log(body[0].virtual); // '5555'

CURL to POST to JIRA

I am using curl command in powershell in my Windows machine. I am trying to create an issue in JIRA which I have installed in my local. I tried following but it throws me error. Can someone let me know what am I missing and how to fix it?
PS C:\Users\raji> **curl -D- -u raji:raji -X POST --data $parse.json -H
"Content-Type: application/json" http://localhost:8080/rest/api/2/issue**
*curl: (6) Could not resolve host: Content-Type
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
X-AREQUESTID: 770x749x1
X-ASEN: SEN-L8183526
Set-Cookie: JSESSIONID=D0B4391C94413FDDB1291C419F3E1360; Path=/; HttpOnly
X-Seraph-LoginReason: OK
Set-Cookie: atlassian.xsrf.token=B3EL-GHY4-P1TP-IMD0|d3d735e0a6566f8a97f99c96e80042551def3192|lin; Path=/
X-ASESSIONID: 1v4terf
X-AUSERNAME: raji
X-Content-Type-Options: nosniff
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Date: Sun, 10 Jul 2016 07:20:36 GMT
*
When I try following I get this error:
PS C:\Users\raji> **curl -D- -u raji:raji -X POST --data #parse.json -H
"Content-Type: application/json" http://localhost:8080/rest/api/2/issue**
*At line:1 char:38
+ curl -D- -u raji:raji -X POST --data #parse.json -H "Content-Type: ap ...
+ ~~~~~~
The splatting operator '#' cannot be used to reference variables in an expression. '#parse' can be used only as an argument to a command. To reference variables in an expression use '$parse'.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : SplattingNotPermitted*
And hence I tried $ instaed of # in file name.
"parse.json" File has following content:
{
"fields": {
"project":
{
"key": "Demo"
},
"summary": "REST ye merry gentlemen",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}
As this is windows machine, I also tried using slash (/) in parse.json file (saw in few posts that / will remove the error) but that also did not help. Please can someone let me know how to fix this?
In the first case
curl -D- -u raji:raji -X POST --data $parse.json -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
$parse is interpreted by powershell as variable, $parse.json as attribute json of the variable $parse, which does not exist, so the command executed would be
curl -D- -u raji:raji -X POST --data -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
data is -H, and the content type header is interpreted as url to access.
In the second case the # in powershell is interpreted as splat operator, if you want a literal # (which is interperted by curl and not by powershell), simply quote the string:
curl -D- -u raji:raji -X POST --data "#parse.json" -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
Now it should use the contents of the file parse.json as data.