我正在尝试通过Restclient的http POST请求将xml发送到另一个Web服务器.这是代码:
response = RestClient.post 'https://secure.rowebooks.co.uk/testorders/orders.aspx', :content_type => "text/xml", :myfile => File.read("#{Rails.root}/public/shared/#{@book}.xml")
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误
ERROR 2 Data at the root level is invalid. Line 1, position 1.ERROR3 Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
我被告知我收到该错误,因为XML文件不在调用的内容中.它必须在内容中.我不知道这是什么意思.
任何建议/线索将不胜感激.
谢谢
我在任何地方都找不到 - 我可以告诉Play!应该(仅)通过HTTP POST访问特定的控制器方法?
像C#的Asp.Net MVC中的HttpPost属性?
public class MyController extends Controller {
@Post
public void addPerson(String name, String address) {
}
}
Run Code Online (Sandbox Code Playgroud)
更新 - 我不明白添加POST路由的做法:
我创建了一个ios应用程序,并希望发表评论文章和本文的评论.但我无法让它发挥作用.
{"error":{"message":"发生了未知错误.","type":"OAuthException","code":1}}
当我发布时 https://graph.facebook.com/{post_id}/comments
{"error":{"message":"(#100)无效的fbid.","type":"OAuthException","code":100}}
当我发布到 https://graph.facebook.com/{commentID}/comments
我的应用程序具有以下网址条目:
url(r'^(?P<pk>\d+)/(?P<action>add|edit)/type/$', 'customer.views.edit', name='customer-edit'),
Run Code Online (Sandbox Code Playgroud)
我想反向发布到该网址。当我执行以下操作时,出现错误NoReverseMatch:
self.client.post(reverse('customer-edit'), {'something:'something'}, follow=True)
Run Code Online (Sandbox Code Playgroud)
这是完整的错误:NoReverseMatch:找不到带有参数“()”和关键字参数“ {}”的“ customer-edit”。
我需要反向传递args或kwargs吗?如果是这样,他们将如何匹配上面的网址?
我陷入了一个不会那么复杂的问题,但我只是没有把事情做对.
假设我有两个型号:
class Notification < ActiveRecord::Base
belongs_to :device
validates :number, presence: true
end
Run Code Online (Sandbox Code Playgroud)
和
class Device < ActiveRecord::Base
belongs_to :user
has_many :notifications, :dependent => :destroy
//rest omitted for brevity
end
Run Code Online (Sandbox Code Playgroud)
使用嵌套路线如下:
resources :devices do
resources :notifications
end
Run Code Online (Sandbox Code Playgroud)
和这样的通知控制器:
class NotificationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_device, :only => [:index, :new, :create]
before_filter :load_notification, only: :create
load_and_authorize_resource :device
load_and_authorize_resource :notification, :through => :device
def index
end
def new
@notification = @device.notifications.build
end
def create
params.each do |param|
logger.debug param
end
@notification …Run Code Online (Sandbox Code Playgroud) 我可以使用StringEntity发布json对象
httppost.setEntity(newStringEntity(obj.toString())); HttpResponse response = httpclient.execute(httppost);
但我必须发布一个文件和json数据,我发现很多Answers使用MultipartEntity但它已被弃用,请建议我使用MultipartEntityBuilder的任何教程或示例代码,谢谢
这是我的代码
Thread t = new Thread(new Runnable() {
@Override
public void run() {
JSONObject jsonobject = null;
String requestURL = "URL";
try {
jsonobject = new JSONObject("{\"first\": \"Test\",\"last\": \"User\",\"name\": \"Test
User\",\"email\": \"tr0121345899@gmail.com\",\"birthdate\": \"1984-01-01\",\"Account\": {\"username\": \"t1r0123425899\",\"password\": \"testuser1234567899\"},\"Address\": {\"postal_code\": \"11230\",\"state_or_province\": \"NY\",\"country\": \"US\"}}");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(requestURL);
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("X-ConsumerApiClient","0f8f0a024d6344e429f5ee96aa66fbfb5c3973b5");
httppost.addHeader("X-ConsumerApiSignature",
"qmt8aEAGRQUvldkDnHw8zgn1kRYuXRDwvmo3TpWlCtE=");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", …Run Code Online (Sandbox Code Playgroud) android json http-post multipartentity apache-httpclient-4.x
我一直在Chrome上使用curl&Postman将带有一个变量的http POST请求发送到我运行的简单HTTP服务器,我注意到他们构建请求的方式略有不同.如果我在构建问题时可能使用了任何不正确的术语,我会道歉 - 我还在学习这些东西.
使用Postman,通过放置'?'来构造请求 资源名称和变量之间.例如
http://192.168.0.2:9999/1/command?a=b
Run Code Online (Sandbox Code Playgroud)
但是,以下curl命令:
curl -X POST http://192.168.0.2:9999/1/command --data a=b
Run Code Online (Sandbox Code Playgroud)
不放'?' 资源名称和变量之间.
结果是HTTP服务器以不同方式解释请求.
在第一种情况下,请求的主体是空的,而在第二种情况下,主体包含a = b.
哪个版本是正确的?
我尝试从一个javascript客户端发送一个POST调用到一个名为addvenue的foursquare API.这是API端点文档链接.
但服务器返回405 - 方法不允许.这是拨打电话的片段
var postdata = {'oauth_token':$scope.access_token_foursquare,
'v':'20141217','name':'randomlisting',
'll':'44.3,37.2','m':'foursquare'};
var req = {
method: 'POST',
url: 'https://api.foursquare.com/v2/venues/add',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
data: postdata
}
$http(req).then(function(response){
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
以下是上述呼叫的请求和响应数据包.
Remote Address:103.245.222.185:443
Request URL:https://api.foursquare.com/v2/venues/add
Request Method:OPTIONS
Status Code:405 Method Not Allowed
**Request Headers**
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:api.foursquare.com
Origin:http://localhost:9000
Referer:http://localhost:9000/foursquare
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
**Response Headers**
Accept-Ranges:bytes
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Length:90
Content-Type:application/json; charset=utf-8
Date:Wed, 17 Dec …Run Code Online (Sandbox Code Playgroud) 如何:body从compojure 安全地获取InputStream 的内容?
我正在尝试使用Friend使用我的环路径进行身份验证,compojure handler/site但是当我尝试:body从http POST请求(这是一个Java InputStream)读取时,它已关闭:
23:01:20.505 ERROR [io.undertow.request] (XNIO-1 task-3) Undertow request failed HttpServerExchange{ POST /paypal/ipn}
java.io.IOException: UT000034: Stream is closed
at io.undertow.io.UndertowInputStream.read(UndertowInputStream.java:84) ~[undertow-core-1.1.0.Final.jar:1.1.0.Final]
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:1.8.0_45]
at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[na:1.8.0_45]
at java.io.BufferedReader.fill(BufferedReader.java:161) ~[na:1.8.0_45]
at java.io.BufferedReader.read(BufferedReader.java:182) ~[na:1.8.0_45]
at clojure.core$slurp.doInvoke(core.clj:6650) ~[clojure-1.7.0-beta1.jar:na]
at clojure.lang.RestFn.invoke(RestFn.java:410) ~[clojure-1.7.0-beta1.jar:na]
Run Code Online (Sandbox Code Playgroud)
如果我删除处理程序,问题就会消失.我找到了一个名为groundhog的可能解决方案,它捕获并存储所有请求.我正在使用的库,clojure-paypal-ipn最初reset在流上调用,但是Undertow(或者其他几个Java/Clojure服务器)不支持它,所以我解决了它.
这是与compojure的作者weavejester的相关讨论.
以下是我的代码的一些片段:
(defroutes routes
...
(POST "/paypal/ipn" [] (payment/paypal-ipn-handler
payment/paypal-data …Run Code Online (Sandbox Code Playgroud) 我试图在Flutter应用程序中使用AWS DynamoDB,并且由于缺少Dart的官方AWS开发工具包,我不得不使用低级HTTP REST API。
签名AWS HTTP请求的方法非常繁琐,但是使用AWS提供的示例作为指南,我能够相对轻松地将Python转换为Dart。最终结果是两组代码均产生相同的auth签名。
当我实际去发送请求时,我的问题就来了。Python可以正常运行,但是发送带有Dart HTTP包的POST会出错
我们计算出的请求签名与您提供的签名不匹配。检查您的AWS Secret Access密钥和签名方法。有关详细信息,请查阅服务文档。
我将为您保留用于生成auth签名的实际代码,因为可以通过发送相同的硬编码请求来简单地复制该问题。请参见下面的Python和Dart代码。
注意:将返回有效的响应
签名已过期:20190307T214900Z现在早于20190307T215809Z(20190307T221309Z-15分钟)
因为请求签名使用当前日期,并且仅有效15分钟。
***** PYTHON代码*****
import requests
headers = {'Content-Type':'application/json',
'X-Amz-Date':'20190307T214900Z',
'X-Amz-Target':'DynamoDB_20120810.GetItem',
'Authorization':'AWS4-HMAC-SHA256 Credential=AKIAJFZWA7QQAQT474EQ/20190307/ap-southeast-2/dynamodb/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=297c5a03c59db6da45bfe2fda6017f89a0a1b2ab6da2bb6e0d838ca40be84320'}
endpoint = 'https://dynamodb.ap-southeast-2.amazonaws.com/'
request_parameters = '{"TableName": "player-exports","Key": {"exportId": {"S": "HG1T"}}}'
r = requests.post(endpoint, data=request_parameters, headers=headers)
print('Response status: %d\n' % r.status_code)
print('Response body: %s\n' % r.text)
Run Code Online (Sandbox Code Playgroud)
***** DART CODE *****
import 'package:http/http.dart' as http;
void main(List<String> arguments) async {
var headers = {'Content-Type':'application/json',
'X-Amz-Date':'20190307T214900Z',
'X-Amz-Target':'DynamoDB_20120810.GetItem',
'Authorization':'AWS4-HMAC-SHA256 Credential=AKIAJFZWA7QQAQT474EQ/20190307/ap-southeast-2/dynamodb/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=297c5a03c59db6da45bfe2fda6017f89a0a1b2ab6da2bb6e0d838ca40be84320'};
var endpoint …Run Code Online (Sandbox Code Playgroud)