Basically, I have a project set up in Restlet which uses JAXRS for mapping resources to paths and uses JAXB for serializing and deserializing XML to/from Java types. I'm currently trying to send a POST request in order to test whether it works, and I'm running into a bit of trouble. Here's my resource:
#Path("stream")
public class StreamResource {
#POST
#Consumes("text/xml")
#Produces("text/xml")
public Stream save(Stream value) {
logger.debug("saving new stream...");
return (Stream)this.streamPersistence.save(value);
}
}
Here's my Stream class:
#XmlRootElement(name="stream")
#XmlType(propOrder={"id", "streamName", "title", "description", fileSystemPath"})
public class Stream {
private Long id;
private String streamName;
private String fileSystemPath;
private String title;
private String description;
// getters/setters omitted for brevity
}
And here's how I'm invoking curl:
curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type:"text/xml"' http://localhost:8888/stream
Here's the error I'm getting from curl:
The given resource variant is not supported.
...and here's the error in Restlet:
15:02:25.809 [Restlet-961410881] WARN org.restlet.Component.Server - Error while parsing entity headers java.lang.IllegalArgumentException: Illegal token: "text
at org.restlet.data.MediaType.normalizeToken(MediaType.java:647)
at org.restlet.data.MediaType.normalizeType(MediaType.java:686)
at org.restlet.data.MediaType.<init>(MediaType.java:795)
at org.restlet.data.MediaType.<init>(MediaType.java:767)
at org.restlet.engine.http.header.ContentTypeReader.createContentType(ContentTypeReader.java:84)
at org.restlet.engine.http.header.ContentTypeReader.readValue(ContentTypeReader.java:112)
at org.restlet.engine.http.header.ContentType.<init>(ContentType.java:99)
at org.restlet.engine.http.header.HeaderUtils.extractEntityHeaders(HeaderUtils.java:664)
at org.restlet.engine.http.connector.Connection.createInboundEntity(Connection.java:313)
at org.restlet.engine.http.connector.ServerConnection.createRequest(ServerConnection.java:136)
at org.restlet.engine.http.connector.ServerConnection.readMessage(ServerConnection.java:229)
at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:673)
at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
What am I doing wrong here? This seems pretty straightforward, right?
Remove the quotes around text/xml.
In other words you want
curl -X POST -d '<stream><streamName>helloWorld.flv</streamName><title>Amazing Stuff, Dude!</title><description>This stream is awesome-cool.</description><fileSystemPath>/home/rfkrocktk/Desktop/helloWorld.flv</fileSystemPath></stream>' --header 'Content-Type: text/xml' http://localhost:8888/stream
curl -v -H "Content-Type: application/xml" -X POST --data-binary "#token.xml" URL
Related
i am new to using curl and I am trying to execute a https post request using curl. and it doest seem to work like other json post requests so i was hoping someone can explain this to me
I am not sure if i understood your answer well enough but you can take a look at this Package or this one the latter one provides provides more flexibility and features like Interceptors etc.
You can use this package Curl https://pub.dev/packages/curl
example code
import 'package:curl/curl.dart';
import 'package:http/http.dart';
final req1 = new Request("GET", "https://exyui.com/endpoint");
print(toCurl(req1));
// will print out:
// curl 'https://exyui.com/endpoint' --compressed --insecure
final req2 = new Request("PUT", "https://exyui.com/endpoint");
req2.body = "This is the text of body😅, \\, \\\\, \\\\\\";
print(req2);
// will print out:
// curl 'https://exyui.com/endpoint' -X PUT -H 'content-type: text/plain; charset=utf-8' --data-binary \$'This is the text of body\\ud83d\\ude05, \\, \\\\, \\\\\\' --compressed --insecure
final req3 = new Request("POST", "https://exyui.com/endpoint");
final part1 = "This is the part one of content";
final part2 = "This is the part two of content😅";
final expectQuery = "part1=This%20is%20the%20part%20one%20of%20content&part2=This%20is%20the%20part%20two%20of%20content%F0%9F%98%85";
req3.bodyFields = {
"part1": part1,
"part2": part2,
};
print(toCurl(req3));
I can't get Spring Data REST to accept a JSON-PATCH content body, what am I doing wrong?
Simple domain:
#Document
public class DomainA {
#Id private String id;
private String atext;
public String getAtext(){ return atext;}
public void setAtext(String str){ atext = str;}
}
Backed by a MondoDB repo:
#RepositoryRestResource
public interface DomainARepository extends MongoRepository<DomainA, String> {}
I want to JSON-PATCH this,like so:
curl -X PATCH -H "Authorization: Basic dXNlcjpQVw=="
-H "Content-Type: application/json-patch+json"
-H "Cache-Control: no-cache"
-d '[{"op":"replace", "path":"/atext", "value":"JSON-PATCHed text"}]'
http://localhost:8080/domainAs/550e169209a5cc0df82c95d4
But when I do I get an exception:
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read an object of type class patchex.DomainA from the request!;
nested exception is
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read payload!; nested exception is java.lang.ClassCastException:
com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to
com.fasterxml.jackson.databind.node.ObjectNode at
org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.readPatch(PersistentEntityResourceHandlerMethodArgumentResolver.java:183)
....
If I supply the patch body without the array brackets, I get back a 204; no exception but no change to the document, either. I can "patch" the document using PUT or PATCH of {"atext":"PUTed value"}; POSTs/GETs work fine; and, the tests in Spring Data REST seem to verify that JSON-PATCH should work if I can get the content body accepted. So, what's the right way to using PATCH here?
(Using Spring Boot 1.2.2.RELEASE starters for -data-mongodb, -data-rest, java 1.8)
EDIT: Same exception if I back with a JPA (H2) CRUD #RepositoryRestResource repo.
EDIT: Opened JIRA at https://jira.spring.io/browse/DATAREST-498
I'm trying to learn Spring MVC and i'm stuck at this program behaving weirdly can any one please help.
#Controller
public class GreetingController {
#RequestMapping(value="/greeting", consumes={"application/json", "application/xml"},produces = {"application/json","application/xml"}, headers = "Content-type=*/*")
public #ResponseBody Greeting greeting(
#RequestParam(value="name", required=false, defaultValue="I'm default") String name) {
Greeting obj = new Greeting("1",name);
Address address = new Address("CA, US");
obj.setAddress(address);
return obj;
}
}
Greeting and Address are just POJO's.
here is the output from 'curl' i.e irrespective of Content-type output is in JSON format.
curl --header "Content-type: application/xml" http://host.com:8080/javamvc/greeting
{"id":1,"content":"Hello, I'm default!","address":{"addr":"CA, US"}}
curl --header "Content-type: application/json" http://host.com:8080/javamvc/greeting
{"id":1,"content":"Hello, I'm default!","address":{"addr":"CA, US"}}
and then when i use 'RestClient' from mozilla i get output as xml always as irrespective of my Content-type=application/json or Content-type=application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><greeting><id>1</id><content>Hello, I'm default!</content><address><addr>CA, US</addr></address></greeting>
Can anyone please help?
In your curl requests, you are specifying the Content-Type of the request body, not the content type you are expecting in the response body.
For that, you need to specify the Accept header.
Spring is always producing application/json, because it's the first in the list of produces values.
You'll have to be more specific about what you are doing with Mozilla, but it seems like it's requesting with the Accept header being application/xml.
I’m playing with GitHub web hooks and I want to use curl to post some sample JSON. The request is supposed to contain a payload POST parameter with the JSON string as a value. As an example, this works:
$ curl --form payload='{"foo":1}' http://somewhere
But I need to read the JSON from a file (say foo.json). Replacing the JSON string by #foo.json posts the payload in the request body:
--------------------------8d0c6d3f9cc7da97
Content-Disposition: form-data; name="payload"; filename="foo.json"
Content-Type: application/octet-stream
{'foo':1}
--------------------------8d0c6d3f9cc7da97--
That’s not really what I want, I need the payload to be passed as a parameter. How do I do that?
Maybe silly but try this:
cat foo.json | xargs -I % curl --form payload='%' http://example.com
Just wrote this little thing and it worked:
var=`cat some.json` && curl --form payload="$var" http://lvh.me/test/index.php
Here’s an alternative Perl testing script. Pass the target URL as the first argument, feed the JSON to STDIN:
#!/usr/bin/env perl
use strict;
use warnings;
use LWP;
my $target = shift #ARGV or die "Need a target URL";
my $ua = LWP::UserAgent->new;
my $payload = do { local $/; <STDIN> };
my $response = $ua->post($target, { payload => $payload });
print $response->as_string;
I am trying to build a springmvc app. On one of the action I want it to take a POST of JSON content. I am sending the following values:
POST /TestResults/create.json HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
[ { "test_name": "value1", "test_time": "111"} ]
However, when I send it I am getting the following error:
The request sent by the client was syntactically incorrect.
My action looks like this:
#RequestMapping(value="/create", method=RequestMethod.POST, consumes = "application/json")
#ResponseBody
public ModelAndView createTest(#RequestBody TestResult test_result) {
When I change the action to bepublic ModelAndView createTest(#RequestBody String test_result) { the action does succeed but I am using "String" at that time.
Is it possible to know what I might be doing incorrectly?
The JSON you are receiving is a JSON array. A JSON array unless you have a custom deserializer cannot be mapped to a type like TestResult, unless it's some subtype of Collection. You'll want to use something like
public ModelAndView createTest(#RequestBody List<TestResult> testResults) {
For #RequestBody String it works because all Spring has to do is read from the request body and convert the bytes into a String.