I can do get on camel-example-swagger-cdi. But how do I do a put ?
I tried the following on browser, it didn't work at all.
http://localhost:8080/user/{'id':'222','name':'Richard'}.
And I tried the following curl command, it didn't work either
curl -X PUT -d id=222 -d name="Richard" localhost:8080/user
Please advise how I can issue a post
public class UserRouteBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
// configure we want to use servlet as the component for the rest DSL
// and we enable json binding mode
restConfiguration().component("netty4-http").bindingMode(RestBindingMode.json)
// and output using pretty print
.dataFormatProperty("prettyPrint", "true")
// setup context path and port number that netty will use
.contextPath("/").port(8080)
// add swagger api-doc out of the box
.apiContextPath("/api-doc")
.apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
// and enable CORS
.apiProperty("cors", "true");
// this user REST service is json only
rest("/user").description("User rest service")
.consumes("application/json").produces("application/json")
.get("/{id}").description("Find user by id").outType(User.class)
.param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
.to("bean:userService?method=getUser(${header.id})")
.put().description("Updates or create a user").type(User.class)
.param().name("body").type(body).description("The user to update or create").endParam()
.to("bean:userService?method=updateUser")
.get("/findAll").description("Find all users").outTypeList(User.class)
.to("bean:userService?method=listUsers");
}
}
I used Postman to do a put:
localhost:8080/user?body={'id':'222' , 'name':'Richard' }
Got
Used Postman to do a put. Got
java.net.URISyntaxException: Illegal character in query at index 11: /user?body=
{%27id%27:%272222%27%20,%20%27name%27:%27Richard%27%20}
I got the following exception when I run curl to put.
$ curl -X PUT -d "body={'id':'222','name':'Richard'}" http://localhost:8080/user
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3142 100 3108 100 34 33419 365 --:--:-- --:--:-- --:--:-- 50129com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'body': was expecting ('true', 'false' or 'null')
at [Source: java.io.ByteArrayInputStream#17b2613c; line: 1, column: 6]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1581)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3451)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2610)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:841)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:737)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3776)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3721)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2796)
at org.apache.camel.component.jackson.JacksonDataFormat.unmarshal(JacksonDataFormat.java:173)
at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69)
at org.apache.camel.processor.binding.RestBindingProcessor.process(RestBindingProcessor.java:168)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.component.netty4.handlers.ServerChannelHandler.processAsynchronously(ServerChannelHandler.java:134)
at org.apache.camel.component.netty4.handlers.ServerChannelHandler.channelRead0(ServerChannelHandler.java:105)
at org.apache.camel.component.netty4.http.handlers.HttpServerChannelHandler.channelRead0(HttpServerChannelHandler.java:211)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler.channelRead0(HttpServerMultiplexChannelHandler.java:113)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)
at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:42)
at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:309)
at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:36)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at java.lang.Thread.run(Thread.java:724)
If you can use GUI tools you should take a look at Postman, otherwise you can run:
curl -X PUT -d "{'id':'222','name':'Richard'}" http://localhost:8080/user
I would also recommend you to take a look at PUT vs POST in REST, and Swagger's Parameter Object docs.
I figured it out. And it is working now. curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" -d "{ \"id\": 222,\"name\": \"Richard\"}" http://localhost:8080/user
Related
I am trying to upload a file to a rest server from jenkins using http plugin. I have a jenkins pipelie where a step involves loading a file(type formData)
to a server using rest.
the server side method uses two parameters:
(#FormDataParam("file") InputStream file, #FormDataParam("fileName") String fileName)
I am using the below method
def filename = "${WORKSPACE}/Test.txt"
data="""{ \"fileName\" : \"Test.txt\" }"""
resp3 = httpRequest consoleLogResponseBody: true,url: "http://<url>",contentType:'APPLICATION_OCTETSTREAM',customHeaders:[[name:'Authorization', value:"Basic ${auth}"]],httpMode: 'POST',multipartName: 'Test.txt',uploadFile: "${filename}",requestBody:data,validResponseCodes: '200'
but when I run the status code is 400 and in the server logs the message is that no filestream and no filename is received i.e not able to get both the arguments.
Please let me know where it is getting wrong
Regards
You can try using curl instead of built-in Jenkins methods:
curl -XPOST http://<url> -H 'Content-Type: application/octet-stream' -H 'Authorization: Basic ${auth}' --data-binary '{\"fileName\" : \"Test.txt\" }'
You can debug it first from within shell. Once it's working, wrap it in sh directive:
sh "curl ..."
Since I was running on windows so bat + curl worked for me .With this workaround I was able to transfer files using jenkins and rest
However using httpRequest from jenkins inbuild library is still not working.
I'm using a wiremock standlaone service to mock several json Object.
Actually I want to mock a pdf file: it works fine on my local by just adding my pdf to the __files folder
However I'm trying to use a POSTMAN (or curl) to push the pdf on the server and this doesnt work :
Here is my curl :
curl -X PUT https://wiremock-mock.myserver.com/__admin/files/Hello_World.pdf
-H 'Accept: */*'-H 'content-type: multipart/form-data'
-F pdf=#///usr/Perso/Downloads/Hello_World.pdf
I'm getting this error:
HTTP ERROR 500
Problem accessing /api/test/fichiers/111111. Reason:
Server Error
Caused by:
wiremock.com.github.jknack.handlebars.HandlebarsException: inline#7adccef6:30:88: found: '?'
#???8FV?c?M8?+?J-P??????????GY???(|?6M:#????w]t???2?P?o?si????2??u?yD]F??$p?
?a????{{z???Z????q????5D&????j}??
??i?s??????{*?r?)5??ee???gzZ?-??kh?'??``Av?2?????(?????Q[????>yYn??iy???vj?f2O?u???gK_???#?7!?c??WF?Y??3$?u ?tT?k)???>??YE???0eX|?V?>?n?-????=?>??j?vB??;?????}2??e?mS?c?D?az??,^?ahV???^??G??R?????\??2?oTjD???G???3??|??9????vnz???&????????M??~?b???????????0???i[v?]?e?"????_????H1m??I?~à;?=???K?.????Ws???? c[?zW9?????]?E:,???????[?^?:????=?????9??V?DF??8?C??+"g$????J??\?nJ?
?Q??n?/?y?????NY?|??????L??3{??c??`C??????N??l??e?????-A??r?4??%???~i?-?????????9ZT!K?i?6)5R???1.????4??6????? B?k2Z????P?6!$??????GFQ????ur?(??(??G?z???d??(????*???J??U??
?R"?b??I?Q?#?? mt??2d??= ????\?6??kO
^
at wiremock.com.github.jknack.handlebars.internal.HbsErrorReporter.syntaxError(HbsErrorReporter.java:93)
at wiremock.org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
at wiremock.com.github.jknack.handlebars.internal.HbsParserFactory$2.notifyListeners(HbsParserFactory.java:148)
at wiremock.org.antlr.v4.runtime.Lexer.nextToken(Lexer.java:144)
at wiremock.org.antlr.v4.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:169)
at wiremock.org.antlr.v4.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:152)
at wiremock.org.antlr.v4.runtime.BufferedTokenStream.consume(BufferedTokenStream.java:136)
at wiremock.org.antlr.v4.runtime.Parser.consume(Parser.java:571)
at wiremock.org.antlr.v4.runtime.Parser.match(Parser.java:203)
at wiremock.com.github.jknack.handlebars.internal.HbsParser.sexpr(HbsParser.java:887)
at wiremock.com.github.jknack.handlebars.internal.HbsParser.var(HbsParser.java:1381)
at wiremock.com.github.jknack.handlebars.internal.HbsParser.statement(HbsParser.java:344)
at wiremock.com.github.jknack.handlebars.internal.HbsParser.body(HbsParser.java:222)
at wiremock.com.github.jknack.handlebars.internal.HbsParser.template(HbsParser.java:165)
at wiremock.com.github.jknack.handlebars.internal.HbsParserFactory$1.parse(HbsParserFactory.java:84)
at wiremock.com.github.jknack.handlebars.cache.NullTemplateCache.get(NullTemplateCache.java:54)
at wiremock.com.github.jknack.handlebars.Handlebars.compile(Handlebars.java:475)
at wiremock.com.github.jknack.handlebars.Handlebars.compileInline(Handlebars.java:435)
at wiremock.com.github.jknack.handlebars.Handlebars.compileInline(Handlebars.java:415)
at com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer.uncheckedCompileTemplate(ResponseTemplateTransformer.java:168)
at com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer.transform(ResponseTemplateTransformer.java:122)
at com.github.tomakehurst.wiremock.stubbing.InMemoryStubMappings.applyTransformations(InMemoryStubMappings.java:91)
at com.github.tomakehurst.wiremock.stubbing.InMemoryStubMappings.serveFor(InMemoryStubMappings.java:72)
at com.github.tomakehurst.wiremock.core.WireMockApp.serveStubFor(WireMockApp.java:167)
at com.github.tomakehurst.wiremock.http.StubRequestHandler.handleRequest(StubRequestHandler.java:50)
at com.github.tomakehurst.wiremock.http.AbstractRequestHandler.handle(AbstractRequestHandler.java:47)
at com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet.service(WireMockHandlerDispatchingServlet.java:108)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at wiremock.org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
at wiremock.org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
at wiremock.org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at wiremock.org.eclipse.jetty.servlets.gzip.GzipHandler.handle(GzipHandler.java:479)
at wiremock.org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
at wiremock.org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at wiremock.org.eclipse.jetty.server.Server.handle(Server.java:499)
at wiremock.org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
at wiremock.org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:258)
at wiremock.org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:748)
How may I push a pdf file on the wiremock server ?
I'm trying to create a nodejs function from a zip file and through the REST API using the following curl:
curl --request PUT --url 'https://my:credentials#openwhisk.eu-gb.bluemix.net/api/v1/namespaces/mynamespace/actions/my_action_name?overwrite=true' --header 'accept: application/json' --header 'content-type: application/json' --data '{"annotations":[{"key":"exec","value":"nodejs:10"},{"key":"web-export","value":true}],"exec":{"kind":"nodejs:10","init":"./action.zip"},"parameters":[{"key":"message","value":"Hello World"}]}'
As a result I get an error:
"error":"The request content was malformed:\n'code' must be a string or attachment object defined in 'exec' for 'nodejs:10' actions"
Is it possible to get an example of how to create a new action from a zip file and through the REST API? Thank you.
You have to base64 encode your .zip file and then pass it as a code parameter. I have written a shell script(bash) to encode and also create an action called 'action'. Save the script as create.sh and execute the script ./create.sh
#!/bin/sh
ACTION=action
ZIP=$ACTION.zip
base64 $ZIP | echo "\"$(cat)\"" | jq "{namespace:\"_\", name:\"$ACTION\", exec:{kind:\"nodejs:10\", code:., binary:true, main:\"main\"}}" | curl -X PUT -H "Content-Type:application/json" -d #- https://USERNAME:PASSWORD#openwhisk.ng.bluemix.net/api/v1/namespaces/_/actions/$ACTION?overwrite=true
Complete code
app.js or index.js code
function myAction(args) {
const leftPad = require("left-pad")
const lines = args.lines || [];
return { padded: lines.map(l => leftPad(l, 30, ".")) }
}
exports.main = myAction;
package.json
{
"name": "node",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"left-pad" : "1.1.3"
}
}
Run npm install and zip the file zip -r action.zip *.
To test the action
ibmcloud fn action invoke --result action --param lines "[\"and now\", \"for something completely\", \"different\" ]"
The REST API for creating or updating a Cloud Functions actions is documented in the IBM Cloud Functions API docs. A good way to find out the exact curl / request syntax is to use the IBM Cloud Functions CLI in verbose mode (-v). The CLI is just a wrapper around the REST API and in verbose mode all the REST details are printed.
Here is the relevant part for what could be printed:
Req Body
Body exceeds 1000 bytes and will be truncated
{"namespace":"_","name":"mytest/myaction","exec":{"kind":"nodejs:8","code":"UEsDBBQAAAAIAHJPhEzjlkxc8wYAAH8VAAALABwAX19tYWluX18ucHlVVAkAA+iFxFrohcRadXgLAAEE9AEAAAT0AQAAxVhtb9s2EP6uX8HRCCLBipb002DA69YkbYo17dZ0GwbDMGSKlrXJokfSToNh/313R+rNL2labJiK1iJ578/x7tTBgJ7A/QzYq8IuN3NmdbpYFIIZm9rC2EKYmiIYsB+1ynW6Ykqz1y9u2WWpNhl7uamELVTFrGJClaUUtha2LeQ9S6uMiVJVspYNgnDPWKVhb5lalqU2ZUXFUqZlmaKwtKTNeWpkzKp0JcsHdj
You would need to set the binary field to true and include the zip content as code. The curl docs suggest to use #filename to reference your zip file:
If you want the contents to be read from a file, use <#filename> as
contents.
One of the endpoints of my API handles PUT request with multipart/form-data. While an audio file is uploaded, some other data is sent from client in form-data. I am able to receive the file using the following code, but am having trouble getting the form-data.
#api.route('/upload')
class AudioUpload(Resource):
def put(self):
now = datetime.now()
filename = now.strftime("%Y%m%d_%H%M%S") + ".mp3"
cwd = os.getcwd()
filepath = os.path.join(cwd, filename)
name = request.form['name']
print('name: ', name, file=sys.stdout)
with open(filepath, 'wb') as f:
f.write(request.stream.read())
return filepath
The curl command I tested with is:
curl -X PUT \
http://localhost:5000/api/upload \
-H 'content-type: multipart/form-data \
-F file=#Audio-3791_244-Feb_04_2018-13_30_04.wav \
-F name=xyz
I got 400 with error The browser (or proxy) sent a request that this server could not understand.
What is the correct way getting form-data in PUT request?
EDIT
Just tried the same code with post. It does not work for getting the form-data either with same error.
I'm new to cloudSQL, trying to build a small console application to test the functionalities of CLoudSQL API. (google-api-services-sqladmin - v1beta4)
Can somebody help me to get start with some sample code?
For example, i want to export data from cloudSQL to GCS using - select query?
Here's a sample HTTP request that will export the table mysql.user to a CSV file in Cloud Storage:
POST https://www.googleapis.com/sql/v1beta4/projects/<project>/instances/<instance>/export
content-type: application/json
content-length: <length-of-request-body>
Authorization: Bearer <access-token>
{
"exportContext": {
"csvExportOptions": {
"selectQuery": "SELECT * FROM mysql.user"
},
"uri": "gs://<bucket>/users.csv",
"fileType": "CSV"
}
}
Note that you need to set the values for <project>, <instance>, <access-token>, and <bucket>.
Once you have those parameters, you can easily try this either using the API Explorer, at the bottom where it says "try it".
Or simply by using curl:
CURL command:
$ curl -X POST \
https://www.googleapis.com/sql/v1beta4/projects/<project>/instances/<instance>/export \
-H'content-length: <length-of-request-body>'
-H'content-type: application/json'
-H'Authorization: Bearer <access-token>'
-d'{"exportContext": {"csvExportOptions": {"selectQery": "SELECT * FROM mysql.user"}, "fileType": "CSV", "uri": "gs://<bucket>/users.csv"}}'