How to use Curl to make a POST request with Dropwizard - rest

I have Dropwizard backend end and like to use curl to make a POST request with a Json body
I used Postman with the settings on the body tab raw radio button selected and on the drop down menu JSON(applicattion/json).I entered the body and was able to successfully create a POST request.
However when I used curl I get an error. I used
curl -v -X POST "localhost:8080/resource"
-H "Content-Type: application/json"
-d '{label1: "words1", label2: "words2"}'
I get the error message
{"code":400,"message":"Unable to process JSON"}
From my understanding it is curl that has an issue since Postman was able to create the POST request.

This
{label1: "words1", label2: "words2"}
is not JSON. Use
{"label1": "words1", "label2": "words2"}

Related

Emoji via CURL in \ PowerShell \ 7 \ are sent to the telegram chat as questions(???) [duplicate]

I would like to POST (send) some form data to a webserver using cURL on a terminal-prompt.
This is what I got so far:
curl --data-ascii "content=derinhält&date=asdf" http://myserverurl.com/api/v1/somemethod
The problem is that the umlaute ("äöü") are replaced by "?" when I receive the post request on the server.
I think I need to use an UTF-8 encoding for the POST request.
Does anybody know how I can achieve this?
You CAN use UTF-8 in the POST request, all you need is to specify the charset in your request.
You should use this request:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" --data-ascii "content=derinhält&date=asdf" http://myserverurl.com/api/v1/somemethod

Jenkins REST API: Create new Job View

I am using the Jenkins REST API to automate our CI process.
The last thing that is missing in the automatization pipeline is the creation of a view if theuser-defined view does not exist.
So far I used:
jenkins_alias/createView?name=My-Test-View
but it is somehow incomplete and returns the error that:
the viewtype should be defined
how can I define this viewtype from the RESTAPI? Should I send it in the Url or in the request body?
thanks
Per this post, this is apparently (a) possible with the REST API and (b) without referencing any weird Hudson stuff.
# create the view
curl -vvv -X POST -d #view.xml -H "Content-Type: text/xml" http://localhost:8080/createView?name=MyView
# get the configuration
curl http://localhost:8080/view/MyView/config.xml
# update the view
curl -X POST -d #view.xml -H "Content-Type: text/xml" http://localhost:8080/view/MyView/config.xml
Caveat emptor - I believe this is undocumented. According to the Jenkins REST API documentation, all the REST API can do is:
retrieve information from Jenkins for programmatic consumption
trigger a new build
create/copy jobs
Please try this request as an example:
curl -X POST -H 'Jenkins-Crumb:<JENKINS-CRUMB>' --form name=test --form mode=hudson.model.ListView --form json='{"name": "test", "mode": "hudson.model.ListView"}' https://<Jenkins-URL>/createView
View types:
ListView: hudson.model.ListView
Dashboard (if plugin Dashboard View is installed): hudson.plugins.view.dashboard.Dashboard
MyView: hudson.model.MyView

How to POST an updated config.xml using Jenkins REST API?

I wanna write a groovy script to bulk update my job configuration using Jenkins REST API. But I am quite confused by its API doc (http://localhost:8080/jenkins/job/my_job_name/api/).
Fetch/Update config.xml
To programmatically obtain config.xml, hit this URL. You can also POST an updated config.xml to the same URL[http://localhost:8080/jenkins/job/my_job_name/config.xml] to programmatically update the configuration of a job.
How am I gonna POST an xml file to an url mentioned above?
The below curl command works for me.
curl --user $USER:$API_TOKEN -X POST http://localhost:8080/job/test/config.xml -H 'Content-Type: application/xml' --data-binary "#mymodifiedlocalconfig.xml"
curl -X POST -H 'Content-Type: application/octet-stream' http://<ip>/job/<job name>/config.xml --user uname:upass --data-binary #./test.xml
Many answers set Content-Type: application/xml, and it didn't work in my Jenkins. I try to set Content-Type: application/octet-stream and it work successfully.
I tried cURL and it worked.
curl -F "file=#updated_config.xml" "http://localhost:8080/jenkins/job/my_job_name/config.xml"
Note: U will need to toggle off "Prevent Cross Site Request Forgery exploits" in Jenkins config.

Uber Rush API Sandbox

Trying to test Uber Rush API (from localhost and from linux server).
Calling Token works - I get the token
trying to implement sanbox example:
curl -X "PUT /v1/sandbox/deliveries/{delivery_id}" \
-H "Authorization: Bearer <OAUTH TOKEN>" \
-d "{\"status\":\"en_route_to_pickup\"}"
with url https://sandbox-api.uber.com/
and I tried the same request with file_get_contents (in PHP)
So, I always get error "405 Method Not Allowed"
{"message":"Method not supported for this endpoint.","code":"method_not_allowed"}
What I need to do to get access to method from this sandbox example https://developer.uber.com/docs/rush/sandbox?
Corrent syntax
curl -X "PUT" -H "Authorization: Bearer <TOKEN>" -H "Content-Type: application/json" -d "{\"status\":\"en_route_to_pickup\"}" https://sandbox-api.uber.com/v1/sandbox/deliveries/DELIVERY_ID
EDIT: Updated to reflect both issues in your question...
You have a mismatch in your requests and an incorrect syntax for curl.
First off your CURL request is incorrectly specified. It should be:
curl -X "PUT" -H "Authorization: Bearer <OAUTH TOKEN>" -d "{\"status\":\"en_route_to_pickup\"}" https://sandbox-api.uber.com/v1/sandbox/deliveries/{delivery_id}
In addition, your curl command is trying to issue a PUT request to the uber sandbox PUT API. However, your PHP code is not setting the context correctly and so is probably issuing a GET request. I suspect that the server is therefore rejecting the request as a GET as not allowed to do this sort of operation.
To fix it, see Bad request using file_get_contents for PUT request in PHP. This should give you an example of how to pass in the necessary context to issue a PUT request using file_get_contents().

POST through SoapUI is unsuccessful, but the same request through DHC is completed successfully

I tested REST services with DHC application for Chrome, and POST requests were successful with these parameters:
DHC_successful_POST
But when I try to create the same request in SoapUI, I always get 500 error. Probably, there should be some other parameters or settings in SoapUI, but I cannot see it. What is wrong? Here is my request:
SoapUI_POST_500_fault
P.S.
In DHC there is such code for my request:
curl -i -X POST \
-H "Content-Type:multipart/form-data" \
-F "file=" \
-F "fileName=rich-text.zip" \
So, I just need to find settings for these parameters in SoapUI (free version).
Are there any suggestions?
As per this (see attachments section)you've to append file: to your parameter value to be able to send the content of file as multipart/form-data