PUT request giving 400 Bad Request Error

落花浮王杯 提交于 2020-01-06 08:51:58

问题


I'm implementing a Contact application using Google Contact API. Now I'm trying to update a contact by sending a put request in the below format

PUT /m8/feeds/contacts/default/full/{contactId}
If-Match: {lastKnownEtag}
GData-Version: 3.0
Content-Type: application/atom+xml

And I have the XML as a string that I'm going to send as a body of the request. This is my xmlString (Body of the Request)

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="*">
<id>http://www.google.com/m8/feeds/contacts/default/base/1785xxxx</id>
<catagory scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:fullname>abc</gd:fullname></gd:name>
<gd:email address="abc@gmail.com" displayName="abc" primary="true" rel="http://schemas.google.com/g/2005#work"/>
<content type="text">Notes</content>
<gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#other">9090xxxxxx</gd:phoneNumber>
</entry>

I have written the below code to send a PUT request to Update a Contact .

    String getUrl = "https://www.google.com/m8/feeds/contacts/default/full/"+contactID+"?oauth_token=" + accessToken;         
    URL url = new URL(getUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();        
    con.setDoOutput(true);          
    con.setRequestMethod("PUT");
    con.setRequestProperty("Content-Type", "application/atom+xml" );
    con.setRequestProperty("GData-Version","3.0"); 
    con.setRequestProperty("IF-MATCH", "*");
    OutputStreamWriter output = new OutputStreamWriter(con.getOutputStream());      
    output.write(xmlString);   
    // xmlString is the body of the request
    output.flush();
    output.close();
    System.out.println(con.getResponseCode());

When I tried to send the request in OAuth 2.0 Playground , the contact is updated successfully. But when I try to run the above program I'm getting

400 Bad Request Error

I don't know where I'm going wrong. Any help would be appreciated!


回答1:


Finally I have found where I'm going wrong.

My xmlString invalid. <entry> tag requires another namespace xmlns="http://www.w3.org/2005/Atom" which is not mentioned in https://developers.google.com/contacts/v3 (Google Contact API). That's the reason why I got 400 Bad request error.

Valid xmlString

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="*" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.google.com/m8/feeds/contacts/default/base/1785xxxx </id>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
    <gd:name>
        <gd:fullName>name</gd:fullName>
    </gd:name>
    <gd:email address="juli@gmail.com" displayName="juli" primary="true" rel="http://schemas.google.com/g/2005#work" />
    <content type="text">Notes</content>
    <gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#other">9090xxxxxx</gd:phoneNumber>
</entry>


来源:https://stackoverflow.com/questions/50932768/put-request-giving-400-bad-request-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!