我正在尝试学习一般的 Web 服务,但有一点疑问。
当发送 HTTP 请求时,如果我在 Accept 标头中提到特定格式(例如 JSON)并假设服务器不支持该格式,那么它是网络调用错误还是服务器仍将以其支持的任何格式响应?
我知道在发送 POST 请求时,客户端按名称“内容类型”发送另一个标头,如果服务器不支持该格式,则它会以 415 错误响应。(不受支持的媒体类型)。
对不起我的英语不好。我有 id 类别列表,我确实提出了这样的要求:
for(Product cat:all_cats) {
new DefaultMediator()
.getProductsInCatalog(cat.getID())
.timeout(15, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(list_products -> {
//do something
},
throwable -> {
Log.e("asd", "asd");
});
}
Run Code Online (Sandbox Code Playgroud)
我觉得不好。我如何使用 rxJava 发送请求列表?
如何使用POST方法发送pandas DataFrame ?
例如,以下拥抱服务器侦听POST请求并使用腌制的熊猫数据帧进行响应:
import hug
import pickle
import traceback
import pandas as pd
@hug.post()
def call(pickle_dump):
print(type(pickle_dump))
try:
df = pickle.loads(pickle_dump)
return pickle.dumps(df.iloc[0])
except:
print(traceback.format_exc())
return pickle.dumps(pd.DataFrame())
Run Code Online (Sandbox Code Playgroud)
当POST发出以下请求时:
import requests
import pandas as pd
df = pd.DataFrame(pd.np.random.randn(10,20))
r = requests.post('http://localhost:8000/call', data = {'pickle_dump':pickle.dumps(df)})
pickle.loads(r.text)
Run Code Online (Sandbox Code Playgroud)
服务器返回此回溯:
<class 'str'>
Traceback (most recent call last):
File "post.py", line 9, in call
df = pickle.loads(pickle_dump)
TypeError: a bytes-like object is required, not 'str'
127.0.0.1 - …Run Code Online (Sandbox Code Playgroud) 如何使用 PHP 读取请求有效负载 (POST) 的内容?
我正在使用 Angular 应用程序发送 POST。
我试过:$_POST,$_FILES和file_get_contents('php://input')...
没有一个工作。
我一直在寻找关于这个主题的一段时间。我正在尝试使用OkHttp. 但是,我收到此错误:
okhttp3.internal.http2.StreamResetException: stream was reset:PROTOCOL_ERROR
val client = OkHttpClient()
val response = client.newCall(
Request.Builder()
.addHeader("Authorization:", "Bearer $firebaseToken")
.url("https://someURL/"+ podcastId.toString())
.build())
.execute()
Run Code Online (Sandbox Code Playgroud)
我不确定这是否与标题URL或我的Authorization标题有关?任何帮助将不胜感激,提前致谢。
是否有一些函数可以将请求参数添加到 http 请求中,而您不必“手动”执行此操作?例如,如果我想"user": "x"作为我的请求的参数,以某种方式实现类似
http:test/testing?user=x
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我正在尝试将 Cookie 添加到我的请求中:
在这里我收到csrftoken一个GET请求:
Future<String> getCsrftoken() async{
var response = await http.get(Uri.encodeFull('http://test/accounts/login/'));
var csrftoken = response.headers.remove('set-cookie').substring(10,74); //csrf
64 chars
return csrftoken;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我尝试使用包Dio执行POST( application/x-www-form-urlencoded) 请求 。
getSessionId() async {
var csrf = await getCsrftoken();
var cj = new CookieJar();
List<Cookie> cookies = [new Cookie("csrftoken", csrf)];
cj.saveFromResponse(Uri.parse("http://test/accounts/login/"), cookies);
List<Cookie> results = cj.loadForRequest(Uri.parse("http://test/accounts/login/"));
var dio = new Dio(new Options(
baseUrl: "http://test/accounts/login/",
connectTimeout: 5000,
receiveTimeout: 100000,
// 5s
headers: {
},
contentType: ContentType.JSON,
// …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,要调用POST我使用的所有请求service。
当我从服务器获取特定代码(例如:401)时,我调用 API 来获取新令牌。
直到收到另一个令牌,如果有任何其他 API 调用,我将所有这些请求存储在一个数组中。它可能是n 个请求。现在假设有 3 个 API 调用,同时对 newToken API 的调用正在进行中。
获得新令牌后,我必须将其传递到所有后续 API 中。现在我必须执行所有待处理的 API 请求并将数据提供给它们各自的调用。
代码示例:
api.service.ts
POST(URL , param){
return new Observable<any>(observer => {
let headers = new HttpHeaders({
'Content-Type': 'Content-Type': 'application/json'
});
let options = {
headers: headers
};
this.http.post(URL, param, options)
.subscribe(data => {
var apiRes: any = data;
this.inValidSession();
observer.next();
observer.complete();
}
......
// For execute pending request I have set …Run Code Online (Sandbox Code Playgroud) 我发现,我的 Laravel (5.7) 应用程序在某些情况下有一个奇怪的行为,它执行两个表单提交请求。上周一切正常。
例如:有一个注册表。当用户想要注册并提交表单时,他无法通过验证,因为输入的电子邮件地址已经存在(在他提交表单之前并不正确)。在我的数据库中一瞥向我展示了用户已创建并登录。因此,表单似乎以某种方式执行了两个请求。
这种行为发生在整个应用程序的几种形式上,但不是全部。这会导致一些非常糟糕的操作(上面的操作,电子邮件发送两次,图像上传两次等)。
一个可能的问题原因可能是一个新的中间件,我在其中访问当前请求并对其执行一些操作:
class CookiebannerMiddleware
{
public function handle(Request $request, Closure $next)
{
$cookie_name = config('cookiebanner.cookie_key');
$cookie_value = config('cookiebanner.cookie_value');
$cookie_lifetime = config('cookiebanner.cookie_lifetime');
$response = $next($request);
if(!$request->hasCookie($cookie_name)){
$response->cookie($cookie_name, $cookie_value, $cookie_lifetime);
return $response;
}
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud)
或者还有其他可能导致此类问题的可能性吗?
更新
问题是我的 CookiebannerMiddleware。该问题已通过以下解决方案解决:
class CookiebannerMiddleware
{
public function handle(Request $request, Closure $next)
{
$cookie_name = config('cookiebanner.cookie_key');
$cookie_value = config('cookiebanner.cookie_value');
$cookie_lifetime = config('cookiebanner.cookie_lifetime');
if(!$request->hasCookie($cookie_name)){
return $next($request)->withCookie($cookie_name, $cookie_value, $cookie_lifetime);
}
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud) 我无法打印结果。如果我使用,这在烧瓶上工作正常,result= request.form然后print(result). 这会在烧瓶上打印字典。但使用瓶子不起作用。当我使用type(result)它时说<class 'bottle.FormsDict'>
x.py 文件:
from bottle import request, template,route,run,post
@route('/')
def index():
return template('val.html')
@post('/result')
def result():
result=request.forms
print(result) #Unable to print
if __name__ == '__main__':
run(host='localhost',port=8080,debug='True',reloader='True')
Run Code Online (Sandbox Code Playgroud)
val.html 文件:
<!DOCTYPE html>
<html>
<body>
<form action="http://localhost:8080/result" method = "POST">
Select a time:
<input type="time" name="usr_time">
<br> <br>
<input type="checkbox" name="A" value="A is on" >A </input>
<br>
<input type="checkbox" name="B" value="B is on" >B </input>
<br>
<input type="checkbox" name="C" value="C is on" >C …Run Code Online (Sandbox Code Playgroud)