如何在bash中处理HTTP 102?

时间:2017-05-03 15:10:11

标签: bash rest curl

来自bash / curl我正在使用一个接收POST的API,执行繁重的长任务并在成功时返回200。由于我们在WAF中面临一些超时,API已经改进以接受标题:

  

- 标题"期望:102-处理"

如果API收到该标头,它每20秒发送一次HTTP 102,直到进程结束并发送HTTP 200.这应该足以防止超时。

我需要做些什么才能处理这些HTTP 102?

我将该标题添加到我的curl命令中,但是一旦收到第一个102,curl命令就会完成。

  • 我在想,也许curl中有一个参数要等到200或者错误。
  • 我想到的另一个选择是在循环中查询状态,但我不知道如何指示curl监视该连接

这是我的bash脚本的测试版。

#!/bin/bash
clear

function_triggerFooAdapter()
{
    pFooUrl=$1
    pPayload=$2
  pCURLConnectTimeout=$3
  pWaitForFooResponse=$4
  pAddExpect102Header=$5

  rm ./tmpResult.html 2>/dev/null
    rm ./tmpResult.txt 2>/dev/null

  echo "Triggering internal Foo adapter $pFooAdapterName"
    echo "Full URL=$pFooUrl"
    echo "Payload to send=$pPayload"
  echo "Curl connect-timeout=$pCURLConnectTimeout"
  echo "WaitForFooResponse=$pWaitForFooResponse"
  echo "AddExpect102Header=$pAddExpect102Header" 

  if [[ "$pAddExpect102Header" = true ]]; then
    text102Header="Expect:102-Processing"
  else
    text102Header="NoExpect;" # send innofensive custom header
  fi

  if [[ "$pWaitForFooResponse" = true ]]; then
    echo "So DO wait..."
    Response=$(curl -k --write-out %{http_code} --header "$text102Header" --header "Content-Type:application/json" --silent --connect-timeout $pCURLConnectTimeout --output ./tmpResult.html -X POST --data "$pPayload" "$pFooUrl" 2>&1 | tee ./tmpResult.txt)
    echo "HTTP Response=$Response"
    echo "$(cat ./tmpResult.txt)"

    if [ "${Response:0:1}" -eq "1" ] || [ "${Response:0:1}" -eq "2" ]; then #if HTTP Response start by 1 or 2 (10x - 20x)...
      echo "Adapter sucessfully triggered."
      return 0
    else
      # cat ./tmpResult.html 2>/dev/null
      #cat ./tmpResult.txt 2>/dev/null
      echo
      echo "HTTP error trying to trigger adapter."
      return 1
    fi
  else
    echo "So DO NOT wait..."
    curl -k --write-out %{http_code} --header "$text102Header" --header "Content-Type:application/json" --silent --connect-timeout $pCURLConnectTimeout --output ./tmpResult.html -X POST --data "$pPayload" "$pFooUrl" > /dev/null 2>&1 &
    echo "Adapter sucessfully (hopefully) triggered. NOT reading HTTP response until Foo code is upgraded to respond directly a HTTP 200 Successfully queued or similar."
    return 0
  fi

}

clear
export http_proxy="http://1.1.1.256:3128/" 
export https_proxy="http://1.1.1.256:3128/"
export no_proxy="foo.com"

# Main
clear
echo "STEP 09- Triggering Foo Internal Adapters."
echo "Proxy settings:"
env | grep proxy

function_triggerFooAdapter "http://foo.com/lookups/trigger_foo" "" 600 true true

1 个答案:

答案 0 :(得分:1)

手动运行并检查 curl -v作为标题发送的内容;我希望看到像

这样的东西
> POST /the/url HTTP/1.1
> Host: thehost.com:80
> User-Agent: curl/7.51.0
> Accept: */*
> Expect: 102-Processing
... some stuff skipped.

如果您没有发送Expect标头;然后卷曲实际上正在做正确的事情..