PUT请求给出400错误请求错误

编程入门 行业动态 更新时间:2024-10-09 19:14:05
本文介绍了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="schemas.google/g/2005" gd:etag="*"> <id>www.google/m8/feeds/contacts/default/base/1785xxxx</id> <catagory scheme="schemas.google/g/2005#kind" term="schemas.google/contact/2008#contact"/> <gd:name> <gd:fullname>abc</gd:fullname></gd:name> <gd:email address="abc@gmail" displayName="abc" primary="true" rel="schemas.google/g/2005#work"/> <content type="text">Notes</content> <gd:phoneNumber primary="true" rel="schemas.google/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 = "www.google/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="www.w3/2005/Atom",在 developers.google中未提及/contacts/v3 ( Google Contact API ). 这就是为什么我收到400错误的请求错误的原因.

My xmlString invalid. <entry> tag requires another namespace xmlns="www.w3/2005/Atom" which is not mentioned in developers.google/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="schemas.google/g/2005" gd:etag="*" xmlns="www.w3/2005/Atom"> <id>www.google/m8/feeds/contacts/default/base/1785xxxx </id> <category scheme="schemas.google/g/2005#kind" term="schemas.google/contact/2008#contact" /> <gd:name> <gd:fullName>name</gd:fullName> </gd:name> <gd:email address="juli@gmail" displayName="juli" primary="true" rel="schemas.google/g/2005#work" /> <content type="text">Notes</content> <gd:phoneNumber primary="true" rel="schemas.google/g/2005#other">9090xxxxxx</gd:phoneNumber> </entry>

更多推荐

PUT请求给出400错误请求错误

本文发布于:2023-10-10 17:55:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   PUT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!