I have a simple Java REST webservice -
@GET
@Path("/get1")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Status getstudent(Track track) {
System.out.println("GET1 title = " + track.getTitle());
System.out.println("GET1 singer = " + track.getSinger());
Status status = new Status();
status.setStatus_flag("success");
return status;
}
Track
public class Track {
String title;
String singer;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
@Override
public String toString() {
return "Track [title=" + title + ", singer=" + singer + "]";
}
}
Python request module:-
import requests
title = {"title":"Best Songs","singer":"lucky"}
r = requests.get("http://localhost:8080/StudentService/rest/insert/get1",data=title)
print r.content
Error
<html><head></head><body>
<h1>HTTP Status 415 - Unsupported Media Type</h1>
<HR size="1" noshade="noshade">
<p><b>type</b> Status report</p>
<p><b>message</b> <u>Unsupported Media Type</u></p>
<p><b>description</b>
<u>The server refused this request because the request entity is
in a format not supported by the requested resource for the requested
method.
</u>
</p>
<HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.41</h3>
</body></html>
Is there a way to create the entity object and send it?
asked Oct 30, 2014 at 22:36
user1050619
21k89 gold badges255 silver badges432 bronze badges
2 Answers 2
You need to use the params to send URL parameters; you are trying to send a request body instead.
You need to alter your service to accept form parameters:
@GET
@Path("/get1")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Status getstudent(
@FormParam("title") String title,
@FormParam("singer") String sing) {
System.out.println("GET1 title = " + title);
System.out.println("GET1 singer = " + singer);
Status status = new Status();
status.setStatus_flag("success");
return status;
}
Now you can send URL-encoded parameters:
params = {"title": "Best Songs", "singer": "lucky"}
r = requests.get(
"http://localhost:8080/StudentService/rest/insert/get1",
params=params)
answered Oct 30, 2014 at 22:40
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Martijn Pieters
@user1050619: Does your Java service expect a JSON request then?
user1050619
Yes, it expects a JSON request now..I have edited the web method @consumes(to accept only JSON request) now...Still not working
Martijn Pieters
@user1050619: I'm not familiar with Java REST servers; no idea if that would require a POST request instead.
Martijn Pieters
@user1050619: looking at vogella.com/tutorials/REST/article.html you should really use
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) and @FormParam to specify the title and singer.Just add content-type and accept in headers. Like:
headers = {"Content-Type": "application/json", "Accept": "application/json"}
r = requests.get("http://localhost:8080/StudentService/rest/insert/get1", data=title, headers=headers)
Comments
Explore related questions
See similar questions with these tags.
default