Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9096717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:55:25+00:00 2026-06-16T23:55:25+00:00

I am trying to post a new resource to a django tastypie API with

  • 0

I am trying to post a new resource to a django tastypie API with a Java Client, but i am getting a Http 500 error code.
Basically i am just trying to make a new reservation on my api from the client.

Models:

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __unicode__(self):
        return self.name

class Product(models.Model):
    author = models.ForeignKey(Author)

    name = models.CharField(max_length=100)
    genre = models.CharField(max_length=100)
    pub_date = models.DateTimeField()

class Reservation(models.Model):
    user = models.ForeignKey(User)
    product = models.ForeignKey(Product)

    reserv_date_start = models.DateTimeField()
    reserv_finish = models.DateTimeField()
    penalty = models.BooleanField()

    def __unicode__(self):
        return self.product.name

Resources:

class AuthorResource(ModelResource):
    #user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        queryset = Author.objects.all()
        resource_name = 'author'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

class ProductResource(ModelResource):
    author = fields.ForeignKey(AuthorResource, 'author')
    class Meta:
        queryset = Product.objects.all()
        resource_name = 'product'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

class ReservationResource(ModelResource):
    product = fields.ForeignKey(ProductResource, 'product')
    class Meta:
        queryset = Reservation.objects.all()
        resource_name = 'reservation'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

My json that i am posting (i am using java simple json and i am getting the backslashes and i don’t know how to take it out):

public JSONObject encodeJsonObject(Reservation reservation){
    JSONObject obj=new JSONObject();
    obj.put("id",String.valueOf(reservation.getId()));
    obj.put("product","/api/reservation/product/"+reservation.getProduct().getId()+"/");      
    obj.put("reserv_date_start",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_date_start()));
    obj.put("reserv_finish",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_finish()));        
    obj.put("resource_uri", "/api/reservation/reservation/"+reservation.getId()+"/");
    obj.put("penalty",reservation.isPenalty());
    return obj;
}

json: {
"product": "\/api\/reservation\/product\/12\/",
"id": "7",
"reserv_finish": "2013-01-05T23:11:51+00:00",
"resource_uri": "\/api\/reservation\/reservation\/7\/",
"penalty": false,
"reserv_date_start": "2013-01-05T23:11:51+00:00"

}


My client to post code:

public void apacheHttpClientPost(String url, String user, char[] pass, JSONObject data) {
     try {

    DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(user, new String(pass)));
    HttpPost postRequest = new HttpPost(url);
    StringEntity input = new StringEntity(data.toJSONString());
    input.setContentType("application/json");
    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 201) {
        throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatusLine().getStatusCode());
    }

    BufferedReader br = new BufferedReader(
                    new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();

  } catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

}

one of the debug errors:

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py:808:
RuntimeWarning: DateTimeField received a naive datetime (2013-01-04
17:31:57) while time zone support is active. RuntimeWarning)

i am posting to

“http://localhost:8000/api/reservation/reservation/”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:55:27+00:00Added an answer on June 16, 2026 at 11:55 pm

    got it. the problem was in my json that was incomplete. i had a foreign key to my user resource that i didn’t add. here is the solution:

    json:

    {
        "product": "\/api\/reservation\/product\/12\/",
        "id": "7",
        "reserv_finish": "2013-01-06T15:26:15+00:00",
        "resource_uri": "\/api\/reservation\/reservation\/7\/",
        "penalty": false,
        "reserv_date_start": "2013-01-06T15:26:15+00:00",
        "user": "\/api\/reservation\/auth\/user\/1\/"
    }
    

    resource:

    class ReservationResource(ModelResource):
        user = fields.ForeignKey(UserResource, 'user')
        product = fields.ForeignKey(ProductResource, 'product')
        class Meta:
            queryset = Reservation.objects.all()
            resource_name = 'reservation'
            authentication = BasicAuthentication()
            authorization = DjangoAuthorization()
    

    java client code:

    public JSONObject encodeJsonObject(Reservation reservation){
        JSONObject obj=new JSONObject();
        obj.put("id",String.valueOf(reservation.getId()));  
        obj.put("product","/api/reservation/product/"+reservation.getProduct().getId()+"/");      
        obj.put("reserv_date_start",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_date_start()));
        obj.put("reserv_finish",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_finish()));        
        obj.put("resource_uri", "/api/reservation/reservation/"+reservation.getId()+"/");
        obj.put("user", "/api/reservation/auth/user/1/"); //not dynamic yet
        obj.put("penalty",reservation.isPenalty());
        return obj;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make creating new instances with Tastypie work, but I keep getting
Im very new in C++ I have found this post http://msdn.microsoft.com/en-us/magazine/cc163486.aspx and trying to
I'm trying to implement a simple Django service with a RESTful API using tastypie.
I am trying to make a POST request to /api/kpi?data=some+stuff : curl -i http://127.0.0.1:9010/api/create_kpi
Getting an error while trying to consume a Restful web service using POST method(with
Help, I'm trying to create a new post in my wordpress blog with custom
I'm trying POST a check-in request in Google Places API. The way they described
Trying to post JSON data to Spring controller.. Not working at all JSP Code:
I'm trying to POST a PayPal or GoogleCheckout buy now button from the code
I'm trying to implement a collection resource with Liberator where a POST request to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.