PUT请求给出400错误请求错误 [英] PUT request giving 400 Bad Request Error

查看:684
本文介绍了PUT请求给出400错误请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Contact API实施Contact应用程序. 现在,我尝试通过发送以下格式的放置请求来更新联系人

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

我将XML作为字符串发送给请求主体. 这是我的xmlString(请求正文)

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>

我已经编写了以下代码来发送PUT请求以更新联系人.

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());

当我尝试在OAuth 2.0 Playground中发送请求时,联系人已成功更新. 但是,当我尝试运行上述程序时,我得到了

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错误的请求错误

400 Bad Request Error

我不知道我要去哪里.任何帮助将不胜感激!

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

推荐答案

最后,我找到了我要去的地方.

Finally I have found where I'm going wrong.

我的xmlString无效. <entry>标记需要另一个名称空间xmlns="http://www.w3.org/2005/Atom",在 https://developers.google.com中未提及/contacts/v3 ( Google Contact API ). 这就是为什么我收到400错误的请求错误的原因.

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.

有效xmlString

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>

这篇关于PUT请求给出400错误请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆