标签: http-post

Java使用apache http将json文件发布到elasticsearch

我可以运行 curl 将文件上传到 elasticsearch curl -XPUT localhost:9200/_bulk --data-binary @other2.json

文件的内容是

{"index":{"_index":"movies","_type":"movie","_id":1}}\n
{"title": "Movie1","director": "director1","year":1962}\n
{"index":{"_index":"movies","_type":"movie","_id":2}}\n
{"title": "Movie2","director": "director2","year": 1972}\n
{"index":{"_index":"movies","_type":"movie","_id":3}}\n
{"title": "Movie3","director": "director3","year": 1972}\n
Run Code Online (Sandbox Code Playgroud)

最后换一行。

但是我无法使用 apache http 客户端发布相同的文件

public static void main(String[] args) throws Exception {
    JsonResponse http = new JsonResponse();

    System.out.println("\nTesting 2 - Send Http POST request");
    http.sendFile();
}
private void sendFile() throws Exception{
    String fileName = "C:\\other2.json";
    File jsonFile = new File(fileName);


    HttpEntity  entity = MultipartEntityBuilder.create()
        .addBinaryBody("file", jsonFile,org.apache.http.entity.ContentType.APPLICATION_JSON, jsonFile.getName())
        .build();

    HttpPost post = new HttpPost("http://localhost:9200/_bulk");
    post.setEntity(entity);

    HttpClientBuilder …
Run Code Online (Sandbox Code Playgroud)

apache json http-post elasticsearch

3
推荐指数
1
解决办法
3472
查看次数

curl POST 未传递 URL 参数

这是我的Java代码:

@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam(value = "x") int x,
        @QueryParam(value = "y") int y) {
    System.out.println("x = " + x);
    System.out.println("y = " + y);
    return (x + y) + "\n";
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼它:

curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x:5&y:3'
Run Code Online (Sandbox Code Playgroud)

问题是System.out.println呼叫一直张贴零零,看来我没有正确传递 x 和 y。

更新

得到答复后,我将请求更改为:

curl   -d '{"x" : 4, "y":3}'  "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -H "Content-Type:application/json" -H "Accept:text/plain"  --include
Run Code Online (Sandbox Code Playgroud)

服务是:

@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String sumPost(@QueryParam(value = "x") int x,
        @QueryParam(value = "y") int y) {
    System.out.println("sumPost");
    System.out.println("x …
Run Code Online (Sandbox Code Playgroud)

java curl jersey http-post

3
推荐指数
1
解决办法
6043
查看次数

使用 fopen 的 Http post 请求

在第一个文件 - 我执行的文件中,我有以下内容:

<?php   
$name = "Julia";
$article = "I like papers";
$url = "http://domain.com/process.php";
$param = array('http' => array(
          'method' => 'POST',
          'content' => $article

        ));

$mad = @stream_context_create($param);
$fp = @fopen($url, 'rb', false, $mad);
$response = @stream_get_contents($fp);
echo $response;
?>
Run Code Online (Sandbox Code Playgroud)

在第二个文件http://domain.com/process.php我有这个:

<?php
$name = $_POST["name"];
$article = $_POST["content"];
$article = $_POST["article"];

echo $article;
echo $name;
echo "Hello there</br>:\n";

?>
Run Code Online (Sandbox Code Playgroud)

我得到的输出只是:

 "Hello there"
Run Code Online (Sandbox Code Playgroud)

那么有什么问题,我如何通过请求传递值 $article 和 $name 以及如何在文件 process.php 中提取它们?

php http-post

3
推荐指数
1
解决办法
5224
查看次数

“HttpRequest”不包含“文件”的定义

我有一种 post 方法,其中有一个文件上传控件。为了在控制器中获取上传的文件,我收到如下错误:

Error   CS1061  'HttpRequest' does not contain a definition for 'Files' and no extension method 'Files' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

[ValidateAntiForgeryToken]
        [HttpPost]
        public async Task<IActionResult> TeamUserDetail(TeamUsers model)
        {
            var file = Request.Files[0];
            return View();
        }
Run Code Online (Sandbox Code Playgroud)

在 Request.Files[0] 它给出了上面显示的错误。项目中使用MVC6。

请指导我。我是否缺少要添加的任何参考?

谢谢你

c# file-upload http-post httprequest

3
推荐指数
1
解决办法
7312
查看次数

带有 F# 的 Kraken 私有 API 返回 EGeneral:无效参数

我正在尝试使用 F# 访问 Kraken 私有 API。访问公共 API 的代码运行得非常好,但是当我尝试访问私有 API 时,我总是收到错误“EGeneral:Invalid arguments”。

#r "FSharp.Data.dll"

open FSharp.Data
open System
open System.Text
open System.Security.Cryptography

let baseUri = "https://api.kraken.com"
let key = MY_KRAKEN_API_KEY
let secret = MY_KRAKEN_API_SECRET
let path = "/0/private/Balance"
let nonce = DateTime.UtcNow.Ticks
let bodyText = "nonce=" + nonce.ToString()

let hmac (key : byte []) (data : byte[]) =
    use hmac = new HMACSHA512(key)
    hmac.ComputeHash(data)

let sha256 (data : string) =
    use sha = SHA256Managed.Create()
    sha.ComputeHash(Encoding.UTF8.GetBytes(data))

let createSignature (nonce : int64) body …
Run Code Online (Sandbox Code Playgroud)

rest f# http-post f#-data

3
推荐指数
1
解决办法
2288
查看次数

如何从 POST 请求附加到 HTML 表单 URL

我想获取 HTML 表单的字段之一并使用它来修改操作的 URL。我关注了这个问题,它显示了它是如何为 GET 请求完成的,但是我在修改 POST 请求的技术时遇到了麻烦。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>User location lookup</title>
    </head>

    <body>
        <!-- method: /sf/ask/826824771/ -->

        <script>
            function process()
            {
                var url="http://localhost:43/" + document.getElementById("url").value;
                location.href=url;
                return false;
            }
        </script>

        <form onSubmit="return process();" method="get">
            Username: <input type="text" name="url" id="url"></input>
            <input type="submit" value="go"></input>
        </form>

        <script>
            function process2()
            {
                var url="http://localhost:43/" + document.getElementById("url").value;
                location.href=url;
                return false;
            }
        </script>

        <form method="post"  onSubmit="return process2();">
            Username: <input type="text" name="url" id="url"> </input>
            Location: …
Run Code Online (Sandbox Code Playgroud)

html javascript http-post

3
推荐指数
1
解决办法
2989
查看次数

Rails 4 不会改变 post 方法来打补丁

我正在尝试提交表单,但如果我只是提交,form_for @classroom我会收到“没有路由匹配 [POST]”错误。

现在使用下面发布的代码,我在表单中得到了错误的 url。如果我在浏览器中手动更改 url,我想我可以通过 javascript 来做到这一点,但是......为什么......这是......这......发生......?

直到昨天一切正常。我什至尝试回滚到我更改的内容,但我似乎无法跟踪出了什么问题。

路由文件

patch 'classrooms/:id/update' => "classrooms#update", as: :update_classroom
resources :classrooms, except: :update
Run Code Online (Sandbox Code Playgroud)

从导轨端形成

<%= form_for(update_classroom_path(@classroom), method: "patch") do |class_f| %>
Run Code Online (Sandbox Code Playgroud)

浏览器中的表单

<form action="/classrooms/23/edit" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="?">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="******">
Run Code Online (Sandbox Code Playgroud)

耙路线

        absences POST   /absences(.:format)                   absences#create
                 POST   /classrooms/:id/getAbsences(.:format) classrooms#getAbsences
update_classroom PATCH  /classrooms/:id/update(.:format)      classrooms#update
      classrooms GET    /classrooms(.:format)                 classrooms#index
                 POST   /classrooms(.:format)                 classrooms#create
   new_classroom GET    /classrooms/new(.:format)             classrooms#new
  edit_classroom GET    /classrooms/:id/edit(.:format)        classrooms#edit
       classroom GET    /classrooms/:id(.:format)             classrooms#show
                 DELETE /classrooms/:id(.:format) …
Run Code Online (Sandbox Code Playgroud)

forms url-routing http-post ruby-on-rails-4

3
推荐指数
1
解决办法
2980
查看次数

使用 React Native 将数组发送到服务器以获取一些数据

我正在尝试使用给定的数据从服务器获取数据。我正在使用以下代码:

    const details = {
      'function': 'getUsers',
    };
    const formBody = 
    Object.keys(details).map(key=>encodeURIComponent(key)+
    '='+encodeURIComponent(details[key])).join('&');
    console.log(formBody);
    fetch(url, {
      method: 'POST',
      credentials: 'include',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'XPIDER_SID=' + this.props.text
      },
      body: formBody
      }).then((response) => response.json())
        .then((responseJson) => {
          if (responseJson.status === 'okay') {
            this.setState({ users: responseJson.users });
          } else {
             this.setState({ error: responseJson.error });
          }
      })
Run Code Online (Sandbox Code Playgroud)

直到现在它都很好用。现在我必须发送一个数据数组,但是当我在细节常量中写入一个数组时,由于 formBody,服务器不接受该数组。在formBody中,整个请求都是字符串,所以数组转为字符串。我怎样才能用这个发送一个数组?或者你们知道其他选择吗?谢谢你 !

arrays api http-post react-native fetch-api

3
推荐指数
1
解决办法
7329
查看次数

如何将 url 参数(查询字符串)传递给 ionic 3 上的 HTTP POST 请求

我不知道如何使用查询字符串调用邮政服务。当我们使用post方法在 url 下运行时:https://test.data.com/myAPI/Login/authenticateUser?username=temp&password=Temp@123®id=''&versions=1&deviceType=1在邮递员中它工作得很好,但不要如何在ionic 3代码中传递查询字符串

在下面的代码中,当我们将查询字符串作为参数传递时,它给出了一个错误的请求错误。

return this.http.post("https://test.data.com/myAPI/Login/authenticateUser", params).subscribe((data: any) => {
  // let resp = data;
  // this.items = JSON.parse(JSON.parse(resp).Data);
  console.log('my data: ', data);
}, err => {
  console.log(err);
});
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

http-post ionic2 ionic3 angular

3
推荐指数
1
解决办法
5160
查看次数

您将如何设计包含 POST 模拟的 REST API?

例如。我需要设计一个具有两个功能的 API:付款验证和付款创建。我需要两者,因为最终用户应该在实际确认付款创建之前知道一切正常。为了创造,我有

POST .../payment
Run Code Online (Sandbox Code Playgroud)

带有输入体。如果出现问题,它会返回 HTTP 400。

验证(或模拟)执行与创建完全相同的过程,在保存数据之前停止该过程。对于这个验证,最好有类似的东西

解决方案1

GET .../is-payment-ok
Run Code Online (Sandbox Code Playgroud)

还有一个输入体。它返回 HTTP 200,包括一个布尔值答案和一些细节。

这里的资源不是付款,而是关于付款有效性的信息,这对我来说似乎是 REST 兼容的。缺点是 API 的用户可能会感到困惑,因为如果付款无效,那么模拟将返回 HTTP 200(主体中的布尔值设置为false),而创建将返回 HTTP 400。

, 或者

解决方案2

POST .../payment?simulation=true , or
POST .../payment-simulation
Run Code Online (Sandbox Code Playgroud)

这里的 HTTP 响应代码与支付创建完全相同。缺点是我们使用 POST,实际上没有“发布”任何资源。

你会怎么做?这种情况下是否有 REST 规则或惯例?

rest http-post

3
推荐指数
1
解决办法
427
查看次数