1

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?

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

@user1050619: Does your Java service expect a JSON request then?
Yes, it expects a JSON request now..I have edited the web method @consumes(to accept only JSON request) now...Still not working
@user1050619: I'm not familiar with Java REST servers; no idea if that would require a POST request instead.
@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.
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.