我有使用http的程序,我想从http读取数据:
data = urllib.request.urlopen(someAddress).read()
Run Code Online (Sandbox Code Playgroud)
并通过readlines()方法为它准备行列表,如返回行.
怎么做?
我正在尝试通过HTML表单将test [key1] = val1和test [key2] = val42发送到服务器.
相应的HTML将是:
<input type="text" name="test[key1]" value="val1" />
<input type="text" name="test[key2]" value="val42" />
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,我想知道这种形式的正确名称.)
>>> import requests, json
>>> params = { 'test' : { 'key1' : 'val1', 'key2' : 'val42' } }
>>> r = requests.post('http://httpbin.org/post', data=params)
>>> json.loads(r.text)['form']
{u'test': [u'key2', u'key1']}
Run Code Online (Sandbox Code Playgroud)
帖子数据已经变平,我们得到了键但丢失了值val1和val42
我正在使用Parse Cloud Code创建一个' DELETE'HTTP请求来删除Iron.io中的多个消息.
它使用与从队列中获取消息的 'GET'请求完全相同的标头和URL :
headers: {
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'OAuth ' + ironToken
},
"获取"的要求做的工作,我是否把method: 'GET'与否里面Parse.Cloud.httpRequest().即使我发送一些数据body:(被忽略)它也能工作.
但是,对于'DELETE'请求,我需要发送正文:
body: {
'ids': ['someMessageId']
}
Run Code Online (Sandbox Code Playgroud)
这个请求失败了,消息非常无益:
{
"status":400,"headers":
{"Access-Control-Allow-Origin":"*",
"Connection":"keep-alive",
"Content-Length":"32",
"Content-Type":"application/json",
"Date":"Tue, 06 May 2014 10:15:27 GMT"
},
"text":"{\"msg\":\"Failed to decode JSON.\"}",
"data":{"msg":"Failed to decode JSON."},
"buffer":[ ...],
"cookies":{}
}
知道为什么会这样,我还能测试什么?
我目前正在研究ASP.NET WebApi和Angularjs
WebApi有一个方法
[System.Web.Http.AcceptVerbs("POST")]
[System.Web.Http.HttpPost]
public HttpResponseMessage SearchAddress(SearchDetails searchDetail)
{
//13.03993,80.231867
try
{
if (!WebSecurity.IsAuthenticated)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NotAcceptable);
return response;
}
List<CollegeAddress> CollegeAddress = addressService.GetAddressFromDistance(17.380498, 78.4864948, 2000);
HttpResponseMessage responseData = Request.CreateResponse(HttpStatusCode.Accepted, CollegeAddress);
return responseData;
}
catch (Exception e)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NotFound);
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
我必须从客户端调用此方法.
当我使用它调用此方法时Ajax,它不起作用,searchDetail如果我使用Ajax ,则method参数始终为null.
$.ajax({
method: 'POST',
url: rootUrl + '/api/Address/SearchAddress',
async: false,
data: searchDetail,
type: "json",
headers: {
'Content-Type': "application/json; charset=utf-8"
}
}).success(function (response) …Run Code Online (Sandbox Code Playgroud) 我正在尝试开发REST服务net/http.
该服务接收包含所有输入参数的JSON结构.我想知道是否有更简单,更短的方式来实现以下内容:
func call(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Printf("Error parsing request %s\n", err)
}
var buf []byte
buf = make([]byte, 256)
var n, err = r.Body.Read(buf)
var decoded map[string]interface{}
err = json.Unmarshal(buf[:n], &decoded)
if err != nil {
fmt.Printf("Error decoding json: %s\n", err)
}
var uid = decoded["uid"]
...
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,只需要提取第一个参数就需要很多行.有任何想法吗?
我正在为Web应用程序内部的第三方api(电子邮件套件)构建一个包装器,可以通过内部和自己的api访问.例如,这些方法将电子邮件地址和订阅列表作为参数并返回结果代码.
基本上我想:
例如成功:
所有这些情况基本上都是2xx类别,但必须触发不同的用户反馈消息,这就是我对使用HTTP状态代码感到不满意的原因.使用纯HTTP状态代码不能提供足够详细的反馈并定义a)附加状态代码或b)完全自定义状态代码感觉如此随机.
那么,去这里的最佳做法是什么?
这个答案表明我应该总是使用标准的HTTP状态代码,如果它们不适用,我的设计是错误的.如果不在客户端使用额外的逻辑和api调用,我如何区分差异?
这个:
$http({method: 'GET', url: '/some/url/returning/json').
success(function(data, status, headers, config) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
表明Angular给了我一个JavaScript对象作为我的success处理程序的第一个参数.据推测,它试图通过嗅探响应的内容类型来变得聪明.
这是不受欢迎的行为.我如何告诉Angular将响应体作为字符串给我?
我对云代码函数"Parse.Cloud.httpRequest"有疑问.我想发送HTTP GET请求与以下curl命令相同.但它似乎无法正常工作.如果您发现错误,请提供帮助.
curl -H "Authorization: token xxx" "https://api.automatic.com/v1/trips"
注意:
我的代码是这样的.然后我访问/旅行.
var express = require('express');
var app = express();
app.set('views', 'cloud/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.get('/trips', function(req, res) {
Parse.Cloud.httpRequest({
url: 'https://api.automatic.com/v1/trips',
headers: {
'Authorization': 'token xxx'
},
success: function (httpResponse) {
console.log(httpResponse.text);
},
error: function (httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
});
});
app.listen();
Run Code Online (Sandbox Code Playgroud)
这是一个日志.
E2014-07-16T04:10:46.102Z] v170: Ran custom endpoint with:
Input: {"method"=>"GET", "url"=>"/trips", "headers"=>{"version"=>"HTTP/1.1", "host"=>"easyparking.parseapp.com", "user-agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, …Run Code Online (Sandbox Code Playgroud) 我正进入(状态
错误405:不允许方法
MessageEnd.java:
package com.example.wordcount;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/jersey")
public class MessageEnd {
@GET
@Path("/getvalue/{word}")
@Produces(MediaType.TEXT_PLAIN)
public Response sayHello(@PathParam("word") String word){
String output = " Count of word " + word;
return Response.status(200).entity(output).build();
}
@PUT
@Path("/sendvalue/{msg}")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response sendMsg(@PathParam("msg") String msg){
String output = "Received message= " + msg;
return Response.status(200).entity(output).build();
}
}
Run Code Online (Sandbox Code Playgroud)
仅供参考,@GET工作正常.
我正在使用以下URI:
http://localhost:8080/message/jersey/sendvalue/hi
Run Code Online (Sandbox Code Playgroud)