nx-http 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/__tests__/app_pan_baidu_1.rb +20 -0
- data/__tests__/app_pan_baidu_2.rb +35 -0
- data/__tests__/app_pan_baidu_3.rb +23 -0
- data/__tests__/app_upload.rb +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example/upload_weibo.rb +34 -0
- data/example/upload_weibo_back.rb +59 -0
- data/example/upload_weibo_http.rb +54 -0
- data/lib/nx/content_type.rb +8 -0
- data/lib/nx/data_transform.rb +23 -0
- data/lib/nx/http.rb +60 -0
- data/lib/nx/version.rb +5 -0
- data/nx-http-0.1.1.gem +0 -0
- data/nx-http-0.1.2.gem +0 -0
- data/nx-http.gemspec +35 -0
- data/package.json +23 -0
- metadata +22 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 337b4a94d37b11617afd4e0fcb69ee0f26c0ad24782123ad9ecf0b56fba87183
|
4
|
+
data.tar.gz: 03770de9d12c3367ae91b4e196a730194aa517c448a888ae7061e868456911ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6e70fd365d548dc2a5925cdc67faf8333963bf28841051a49467ff5811263192a1d38ce153aafe552d4461676eb305591dee849809fde2980aaedd31d89e8aa
|
7
|
+
data.tar.gz: 0fc150f12773deeca9b2f8544a435c58f5e2a80602e5ed692002e49c7c21b068b8e1723d178a8beb43c4aa772cb5ba426b7b32bf713b71d074b5040ab9d61743
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# nx-http
|
2
|
+
> Http for ruby gem.
|
3
|
+
|
4
|
+
## installation
|
5
|
+
```rb
|
6
|
+
# from gem
|
7
|
+
gem 'nx-http'
|
8
|
+
# from git
|
9
|
+
gem 'nx-http', git: 'git@github.com:afeiship/nx-http.git'
|
10
|
+
```
|
11
|
+
|
12
|
+
## usage
|
13
|
+
```rb
|
14
|
+
Nx::Http::request
|
15
|
+
|
16
|
+
## ContentType:
|
17
|
+
Nx::ContentType.constants
|
18
|
+
# [:JSON, :URLENCODED, :MULTIPART]
|
19
|
+
|
20
|
+
## DataTransform
|
21
|
+
Nx::DataTransform.multipart(name:'fei', age:100)
|
22
|
+
# [[:name, "fei"], [:age, 100]]
|
23
|
+
Nx::DataTransform.urlencoded(name:'fei', age:100)
|
24
|
+
# "name=fei&age=100"
|
25
|
+
Nx::DataTransform.json(name:'fei', age:100)
|
26
|
+
# "{\"name\":\"fei\",\"age\":100}"
|
27
|
+
```
|
28
|
+
|
29
|
+
## build/publish
|
30
|
+
```shell
|
31
|
+
# build
|
32
|
+
gem build nx-http.gemspec
|
33
|
+
|
34
|
+
# publish
|
35
|
+
gem push nx-http-0.1.0.gem
|
36
|
+
```
|
37
|
+
|
38
|
+
## resources
|
39
|
+
- https://www.cnblogs.com/wf0117/p/9000443.html
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require_relative "../lib/nx-http"
|
3
|
+
|
4
|
+
# p Nx::ContentType::JSON
|
5
|
+
cookie = "PANWEB=1; csrfToken=HGPEF5J4BMUKDsBEOzo8FNRr; recommendTime=mac2020-03-06%2016%3A33%3A00; BAIDUID=0FEE32A9EDE9E7AA4EFBDC31B4C77E13:FG=1; PSTM=1583562584; H_PS_PSSID=30963_1445_21112_30999_30824_30717; BIDUPSID=33A5F43CBC872598047FC87C5873703E; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a03335825322; BDCLND=dfnU1OnEre564hYcmekBl73OTHnblXDwrOfClJGfkSI%3D; BDUSS=2l6cmprVUJ2N0xHdml-cDByRktMV2JuMGlnOFNRcEhWMm1xWFkyN2NNaFpISXhlRUFBQUFBJCQAAAAAAAAAAAEAAAC-ENsAR19obWxzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmPZF5Zj2ReT; STOKEN=6eed028cbe604e168a978284a4e5ada0d2fb5ad02559cd8568eaddc0d6ac7e17; SCRC=abae33dddbd039188dfca44946e36bc6; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1583507142,1583570220,1583648604,1583692043; delPer=0; PSINO=3; cflag=13%3A3; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1583697935; PANPSC=1634889871415495715%3AZdTG9lVgpntugIGw18cSZVcS2d9ns3O5UnffaEdZJMioCxQgjGERckHdjv8XIXMbF%2FYxkwK%2FsNyX7wZMFzyi3WtpABU2m%2FElQjQ1mFHTd8V1tt%2Bk9Cane%2BomA%2FKeZTApdS8c7ndIzLGm7Wp98loeBrZmQ9Xq9%2FjrmC0l4QVrtZD2VG7Ee3wInG344ihw4UqP9iEEfXjbSguSsVZaqyA8TA%3D%3D; HttpOnly"
|
6
|
+
|
7
|
+
url = URI("https://pan.baidu.com/api/precreate")
|
8
|
+
http = Net::HTTP.new(url.host, url.port)
|
9
|
+
http.use_ssl = true
|
10
|
+
|
11
|
+
request = Net::HTTP::Post.new(url.path)
|
12
|
+
request["Content-Type"] = "application/x-www-form-urlencoded"
|
13
|
+
request["Cookie"] = cookie
|
14
|
+
request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
|
15
|
+
request.body = "path=/db.file&autoinit=1&block_list=%5B%225910a591dd8fc18c32a8f3df4fdc1761%22%5D"
|
16
|
+
|
17
|
+
response = http.request(request)
|
18
|
+
|
19
|
+
# response = http.post(url, { pic1: HTTP::FormData::File.new(tempfile) }, headers)
|
20
|
+
puts response.body
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "pathname"
|
3
|
+
require_relative "../lib/nx-http"
|
4
|
+
|
5
|
+
# p Nx::ContentType::JSON
|
6
|
+
cookie = "PANWEB=1; csrfToken=HGPEF5J4BMUKDsBEOzo8FNRr; recommendTime=mac2020-03-06%2016%3A33%3A00; BAIDUID=0FEE32A9EDE9E7AA4EFBDC31B4C77E13:FG=1; PSTM=1583562584; H_PS_PSSID=30963_1445_21112_30999_30824_30717; BIDUPSID=33A5F43CBC872598047FC87C5873703E; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a03335825322; BDCLND=dfnU1OnEre564hYcmekBl73OTHnblXDwrOfClJGfkSI%3D; BDUSS=2l6cmprVUJ2N0xHdml-cDByRktMV2JuMGlnOFNRcEhWMm1xWFkyN2NNaFpISXhlRUFBQUFBJCQAAAAAAAAAAAEAAAC-ENsAR19obWxzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmPZF5Zj2ReT; STOKEN=6eed028cbe604e168a978284a4e5ada0d2fb5ad02559cd8568eaddc0d6ac7e17; SCRC=abae33dddbd039188dfca44946e36bc6; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1583507142,1583570220,1583648604,1583692043; delPer=0; PSINO=3; cflag=13%3A3; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1583697935; PANPSC=1634889871415495715%3AZdTG9lVgpntugIGw18cSZVcS2d9ns3O5UnffaEdZJMioCxQgjGERckHdjv8XIXMbF%2FYxkwK%2FsNyX7wZMFzyi3WtpABU2m%2FElQjQ1mFHTd8V1tt%2Bk9Cane%2BomA%2FKeZTApdS8c7ndIzLGm7Wp98loeBrZmQ9Xq9%2FjrmC0l4QVrtZD2VG7Ee3wInG344ihw4UqP9iEEfXjbSguSsVZaqyA8TA%3D%3D; HttpOnly"
|
7
|
+
|
8
|
+
tempfile = open("https://tva1.sinaimg.cn/large/da432263gy1gcj74ifcokj205f06gjre.jpg")
|
9
|
+
file2 = Pathname.new("/Users/feizheng/Downloads/analystai-pc/index.js").open
|
10
|
+
uploadid = "N1-MTE5LjM2LjEwLjIyMToxNTgzNzEwMzg3OjE1ODQ0MTAwMzc2OTQ3ODY4ODI="
|
11
|
+
url = URI("https://c3.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&app_id=250528&path=/&uploadid=#{uploadid}&uploadsign=0&partseq=0")
|
12
|
+
http = Net::HTTP.new(url.host, url.port)
|
13
|
+
http.use_ssl = true
|
14
|
+
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
15
|
+
|
16
|
+
data = [
|
17
|
+
["file", file2],
|
18
|
+
]
|
19
|
+
|
20
|
+
request = Net::HTTP::Post.new("https://c3.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&app_id=250528&path=/db.file&uploadid=#{uploadid}&uploadsign=0&partseq=0")
|
21
|
+
request.set_form(data, "multipart/form-data")
|
22
|
+
request["Cookie"] = cookie
|
23
|
+
request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
|
24
|
+
response = http.request(request)
|
25
|
+
|
26
|
+
# # # response = http.post(url, { pic1: HTTP::FormData::File.new(tempfile) }, headers)
|
27
|
+
puts response.body
|
28
|
+
# uploadid = "N1-MTE5LjM2LjEwLjIyMToxNTgzNzAwNjIzOjE1ODE3ODg5OTEyODAyMzQ0MzU="
|
29
|
+
# tempfile = open("https://tva1.sinaimg.cn/large/da432263gy1gcj74ifcokj205f06gjre.jpg")
|
30
|
+
# res = Nx::Http.upload("https://c3.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&app_id=250528&path=/&uploadid=#{uploadid}&uploadsign=0&partseq=0", { file: tempfile }) do |http, request|
|
31
|
+
# request["Cookie"] = cookie
|
32
|
+
# request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
|
33
|
+
# end
|
34
|
+
|
35
|
+
# p res.body
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "digest/md5"
|
3
|
+
require_relative "../lib/nx-http"
|
4
|
+
|
5
|
+
# p Nx::ContentType::JSON
|
6
|
+
cookie = "PANWEB=1; csrfToken=HGPEF5J4BMUKDsBEOzo8FNRr; recommendTime=mac2020-03-06%2016%3A33%3A00; BAIDUID=0FEE32A9EDE9E7AA4EFBDC31B4C77E13:FG=1; PSTM=1583562584; H_PS_PSSID=30963_1445_21112_30999_30824_30717; BIDUPSID=33A5F43CBC872598047FC87C5873703E; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a03335825322; BDCLND=dfnU1OnEre564hYcmekBl73OTHnblXDwrOfClJGfkSI%3D; BDUSS=2l6cmprVUJ2N0xHdml-cDByRktMV2JuMGlnOFNRcEhWMm1xWFkyN2NNaFpISXhlRUFBQUFBJCQAAAAAAAAAAAEAAAC-ENsAR19obWxzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmPZF5Zj2ReT; STOKEN=6eed028cbe604e168a978284a4e5ada0d2fb5ad02559cd8568eaddc0d6ac7e17; SCRC=abae33dddbd039188dfca44946e36bc6; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1583507142,1583570220,1583648604,1583692043; delPer=0; PSINO=3; cflag=13%3A3; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1583694769; PANPSC=10643410382743916518%3ACU2JWesajwC128%2FKujOIXDuv496EomYGD0bORsxZL%2B4I5%2BB0eenXj%2Bhn5LMqRLtxS%2FCwMJZcAlBSd99oR1kkyKgLFCCMYRFyDtTCc6ZqWjETGPk1jKCnAXv4wccwpplt0v29HoOyBxO7W09FU%2BvrLt8NRd7EA5d%2B2fNZfjs7wBY%2FcoIBUQpA2juoAeCl9TBG"
|
7
|
+
uploadid = "N1-MTE5LjM2LjEwLjIyMToxNTgzNzEwMzg3OjE1ODQ0MTAwMzc2OTQ3ODY4ODI="
|
8
|
+
|
9
|
+
url = URI("https://pan.baidu.com/api/create?bdstoken=1c1c379d86804cbf272d8f5513b497b4")
|
10
|
+
http = Net::HTTP.new(url.host, url.port)
|
11
|
+
http.use_ssl = true
|
12
|
+
|
13
|
+
request = Net::HTTP::Post.new("https://pan.baidu.com/api/create?bdstoken=1c1c379d86804cbf272d8f5513b497b4")
|
14
|
+
request["Content-Type"] = "text/plain"
|
15
|
+
# request["Content-Type"] = "application/x-www-form-urlencoded"
|
16
|
+
request["Cookie"] = cookie
|
17
|
+
request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
|
18
|
+
request.body = "path=/52doc.com/index.js&size=839&uploadid=N1-MTE5LjM2LjEwLjIyMToxNTgzNzEwMzg3OjE1ODQ0MTAwMzc2OTQ3ODY4ODI=&block_list=%5B%22360b9e09ac21efbabb906f1115578385%22%5D"
|
19
|
+
|
20
|
+
response = http.request(request)
|
21
|
+
|
22
|
+
# response = http.post(url, { pic1: HTTP::FormData::File.new(tempfile) }, headers)
|
23
|
+
puts response.body
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require_relative "../lib/nx-http"
|
3
|
+
|
4
|
+
# p Nx::ContentType::JSON
|
5
|
+
tempfile = open("https://tva1.sinaimg.cn/large/da432263gy1gcj74ifcokj205f06gjre.jpg")
|
6
|
+
res = Nx::Http.upload("https://picupload.weibo.com/interface/pic_upload.php", { pic1: tempfile }) do |http, request|
|
7
|
+
request["Cookie"] = "SUB=_2A25zZYAzDeRhGeVI7VMZ8yzEyz-IHXVQEvb7rDV8PUNbmtAKLVPMkW9NTBpP0VsITWW-3vSzKJ0qcv_7qLmry_H2; Path=/; Domain=.weibo.com; HttpOnly"
|
8
|
+
end
|
9
|
+
|
10
|
+
p res.body
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nx-http"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "open-uri"
|
3
|
+
require "uri"
|
4
|
+
require "base64"
|
5
|
+
require "json"
|
6
|
+
require "net/http"
|
7
|
+
require "formdata"
|
8
|
+
|
9
|
+
# new
|
10
|
+
require "rest-client"
|
11
|
+
require "http"
|
12
|
+
|
13
|
+
POST_URL = "https://picupload.weibo.com/interface/pic_upload.php"
|
14
|
+
tempfile1 = open("https://tva1.sinaimg.cn/large/da432263gy1gcj74ifcokj205f06gjre.jpg")
|
15
|
+
tempfile2 = open("https://tva1.sinaimg.cn/large/da432263gy1gck7gvo1uvj205f06g0sk.jpg")
|
16
|
+
# p tempfile.class
|
17
|
+
|
18
|
+
headers = {
|
19
|
+
'Content-Type': "multipart/form-data",
|
20
|
+
"Cookie": "SUB=_2A25zW6CKDeRhGeVI7VMZ8yzEyz-IHXVQEJVCrDV8PUNbmtAKLXD7kW9NTBpP0XLSa8hbBJPRXfLo8iEoY3rbDnm1; Path=/; Domain=.weibo.com; HttpOnly",
|
21
|
+
}
|
22
|
+
|
23
|
+
url = URI("http://httpbin.org/post")
|
24
|
+
http = Net::HTTP.new(url.host, url.port)
|
25
|
+
|
26
|
+
# 设置请求参数
|
27
|
+
data = [
|
28
|
+
["name", "test"],
|
29
|
+
["image", open("test.jpg"],
|
30
|
+
]
|
31
|
+
request = Net::HTTP::Post.new(url.path)
|
32
|
+
request.set_form(data, "multipart/form-data")
|
33
|
+
response = http.request(request)
|
34
|
+
puts response.body
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "open-uri"
|
3
|
+
require "uri"
|
4
|
+
require "base64"
|
5
|
+
require "json"
|
6
|
+
require "nx"
|
7
|
+
|
8
|
+
# new
|
9
|
+
require "rest-client"
|
10
|
+
require "http"
|
11
|
+
|
12
|
+
POST_URL = "https://picupload.weibo.com/interface/pic_upload.php"
|
13
|
+
|
14
|
+
# file_path = "./52doc_logo.png"
|
15
|
+
# res = File.read file_path
|
16
|
+
# p res.class
|
17
|
+
|
18
|
+
tempfile = open("https://tva1.sinaimg.cn/large/da432263gy1gcj74ifcokj205f06gjre.jpg")
|
19
|
+
formfile = HTTP::FormData::File.new(tempfile)
|
20
|
+
# f.append("pic1", tempfile)
|
21
|
+
|
22
|
+
headers = {
|
23
|
+
'Content-Type': "multipart/form-data",
|
24
|
+
"Cookie": "SUB=_2A25zZYAzDeRhGeVI7VMZ8yzEyz-IHXVQEvb7rDV8PUNbmtAKLVPMkW9NTBpP0VsITWW-3vSzKJ0qcv_7qLmry_H2; Path=/; Domain=.weibo.com; HttpOnly",
|
25
|
+
}
|
26
|
+
|
27
|
+
res = RestClient.post(POST_URL, { pic1: formfile }, headers)
|
28
|
+
|
29
|
+
data_mock = {
|
30
|
+
"code" => "A00006",
|
31
|
+
"data" => {
|
32
|
+
"count" => 1,
|
33
|
+
"data" => "eyJ1aWQiOjM2NjE4MzI4MDMsImFwcCI6Im1pbmlibG9nIiwiY291bnQiOjEsInRpbWUiOjE1ODM0MDc5MzUuNDI5LCJwaWNzIjp7InBpY18xIjp7IndpZHRoIjoxOTUsInNpemUiOjQxMjUsInJldCI6MSwiaGVpZ2h0IjoyMzIsIm5hbWUiOiJwaWNfMSIsInBpZCI6ImRhNDMyMjYzZ3kxZ2NqYTQyd3E1amoyMDVmMDZnMHNrIn19fQ==",
|
34
|
+
"pics" => {
|
35
|
+
"pic_1" => {
|
36
|
+
"width" => 195,
|
37
|
+
"size" => 4125,
|
38
|
+
"ret" => 1,
|
39
|
+
"height" => 232,
|
40
|
+
"name" => "pic_1",
|
41
|
+
"pid" => "da432263gy1gcja42wq5jj205f06g0sk",
|
42
|
+
},
|
43
|
+
},
|
44
|
+
},
|
45
|
+
}
|
46
|
+
|
47
|
+
data = JSON.parse res.body.split("\n")[2]
|
48
|
+
name = data.get("data.pics.pic_1.pid")
|
49
|
+
p "https://tva1.sinaimg.cn/large/#{name}.jpg"
|
50
|
+
|
51
|
+
# http.use_ssl = true
|
52
|
+
# # 设置请求头
|
53
|
+
# header = {
|
54
|
+
# 'Content-Type': "multipart/form-data",
|
55
|
+
# "Cookie": "SUB=_2A25zW6CKDeRhGeVI7VMZ8yzEyz-IHXVQEJVCrDV8PUNbmtAKLXD7kW9NTBpP0XLSa8hbBJPRXfLo8iEoY3rbDnm1; Path=/; Domain=.weibo.com; HttpOnly",
|
56
|
+
# }
|
57
|
+
# response = http.post(url, { pic1: f }, header)
|
58
|
+
|
59
|
+
# puts response.body
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "open-uri"
|
3
|
+
require "uri"
|
4
|
+
require "base64"
|
5
|
+
require "json"
|
6
|
+
require "net/http"
|
7
|
+
require "formdata"
|
8
|
+
|
9
|
+
# new
|
10
|
+
require "rest-client"
|
11
|
+
require "http"
|
12
|
+
|
13
|
+
POST_URL = "https://picupload.weibo.com/interface/pic_upload.php"
|
14
|
+
tempfile1 = open("https://tva1.sinaimg.cn/large/da432263gy1gcj74ifcokj205f06gjre.jpg")
|
15
|
+
tempfile2 = open("https://tva1.sinaimg.cn/large/da432263gy1gck7gvo1uvj205f06g0sk.jpg")
|
16
|
+
# p tempfile.class
|
17
|
+
|
18
|
+
headers = {
|
19
|
+
'Content-Type': "multipart/form-data",
|
20
|
+
"Cookie": "SUB=_2A25zW6CKDeRhGeVI7VMZ8yzEyz-IHXVQEJVCrDV8PUNbmtAKLXD7kW9NTBpP0XLSa8hbBJPRXfLo8iEoY3rbDnm1; Path=/; Domain=.weibo.com; HttpOnly",
|
21
|
+
}
|
22
|
+
|
23
|
+
# res = RestClient.post(POST_URL, { pic1: HTTP::FormData::File.new(tempfile) }, headers)
|
24
|
+
# p JSON.parse res.body.split("\n")[2]
|
25
|
+
|
26
|
+
# url = URI(POST_URL)
|
27
|
+
# f = FormData.new
|
28
|
+
# f.append("pic1", tempfile)
|
29
|
+
# # filedata = [
|
30
|
+
# # ["pic1", tempfile],
|
31
|
+
# # ]
|
32
|
+
|
33
|
+
# http = Net::HTTP.new(url.host, url.port)
|
34
|
+
# http.use_ssl = true
|
35
|
+
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
36
|
+
# response = http.post_form(url, f, headers)
|
37
|
+
# puts response.body
|
38
|
+
|
39
|
+
# multipart/form-data
|
40
|
+
url = URI(POST_URL)
|
41
|
+
http = Net::HTTP.new(url.host, url.port)
|
42
|
+
http.use_ssl = true
|
43
|
+
# 设置请求参数
|
44
|
+
data = [
|
45
|
+
["pic1", tempfile1],
|
46
|
+
["pic2", tempfile2],
|
47
|
+
]
|
48
|
+
request = Net::HTTP::Post.new(url.path)
|
49
|
+
request.set_form(data, "multipart/form-data")
|
50
|
+
# p request.methods
|
51
|
+
request["Cookie"] = "SUB=_2A25zZYAzDeRhGeVI7VMZ8yzEyz-IHXVQEvb7rDV8PUNbmtAKLVPMkW9NTBpP0VsITWW-3vSzKJ0qcv_7qLmry_H2; Path=/; Domain=.weibo.com; HttpOnly"
|
52
|
+
|
53
|
+
response = http.request(request)
|
54
|
+
puts response.body
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Nx
|
2
|
+
class DataTransform
|
3
|
+
def self.raw(data)
|
4
|
+
data
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.json(data)
|
8
|
+
data.to_json
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.urlencoded(data)
|
12
|
+
URI.encode_www_form(data)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.multipart(data)
|
16
|
+
res = []
|
17
|
+
data.each do |key, value|
|
18
|
+
res << [key.to_s, value]
|
19
|
+
end
|
20
|
+
res
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/nx/http.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Nx
|
2
|
+
class Http
|
3
|
+
def self.upload(in_url, in_data)
|
4
|
+
url = URI(in_url)
|
5
|
+
http = Net::HTTP.new(url.host, url.port)
|
6
|
+
http.use_ssl = url.scheme == "https"
|
7
|
+
|
8
|
+
data = DataTransform.multipart(in_data)
|
9
|
+
request = Net::HTTP::Post.new(url.path)
|
10
|
+
request.set_form(data, ContentType::MULTIPART)
|
11
|
+
if block_given?
|
12
|
+
yield(http, request)
|
13
|
+
end
|
14
|
+
http.request(request)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.request(in_method, in_url, in_data = {}, in_options = {})
|
18
|
+
# uri:
|
19
|
+
uri = URI(in_url)
|
20
|
+
method = in_method.downcase
|
21
|
+
|
22
|
+
# http:
|
23
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
24
|
+
http.use_ssl = uri.scheme == "https"
|
25
|
+
|
26
|
+
# request:
|
27
|
+
method_class = Net::HTTP.const_get method.capitalize
|
28
|
+
request = method_class.new(uri)
|
29
|
+
|
30
|
+
# callback area:
|
31
|
+
if method == "get"
|
32
|
+
uri.query = URI.encode_www_form(in_data)
|
33
|
+
else
|
34
|
+
in_options.each do |key, value|
|
35
|
+
if key == "content_type"
|
36
|
+
ContentType.const_get value.upcase
|
37
|
+
else
|
38
|
+
request[key] = value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
yield(uri, method, request, http)
|
44
|
+
|
45
|
+
begin
|
46
|
+
http.request(request)
|
47
|
+
rescue => exception
|
48
|
+
raise exception
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class << self
|
53
|
+
["get", "post", "put", "delete", "options"].each do |item|
|
54
|
+
define_method item.to_sym do |url, data, options|
|
55
|
+
self.request(item, url, data || {}, options || {})
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/nx/version.rb
ADDED
data/nx-http-0.1.1.gem
ADDED
Binary file
|
data/nx-http-0.1.2.gem
ADDED
Binary file
|
data/nx-http.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nx/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nx-http"
|
8
|
+
spec.version = Nx::Http::VERSION
|
9
|
+
spec.licenses = ["MIT"]
|
10
|
+
spec.authors = ["afeiship"]
|
11
|
+
spec.email = ["1290657123@qq.com"]
|
12
|
+
|
13
|
+
spec.summary = %q{Http for ruby.}
|
14
|
+
spec.description = %q{Http for ruby gem.}
|
15
|
+
spec.homepage = "https://github.com/afeiship/nx-http"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
34
|
+
spec.add_development_dependency "rake", "~> 12.3", ">= 12.3.3"
|
35
|
+
end
|
data/package.json
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
{
|
2
|
+
"name": "nx-http",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "Http for ruby gem.",
|
5
|
+
"directories": {
|
6
|
+
"lib": "lib"
|
7
|
+
},
|
8
|
+
"scripts": {
|
9
|
+
"clean": "rm -rf *.gem",
|
10
|
+
"build": "gem build *.gemspec",
|
11
|
+
"pubpush": "gem push *.gem"
|
12
|
+
},
|
13
|
+
"repository": {
|
14
|
+
"type": "git",
|
15
|
+
"url": "git+https://github.com/afeiship/nx-http.git"
|
16
|
+
},
|
17
|
+
"author": "afei",
|
18
|
+
"license": "MIT",
|
19
|
+
"bugs": {
|
20
|
+
"url": "https://github.com/afeiship/nx-http/issues"
|
21
|
+
},
|
22
|
+
"homepage": "https://github.com/afeiship/nx-http#readme"
|
23
|
+
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nx-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- afeiship
|
@@ -51,7 +51,28 @@ executables: []
|
|
51
51
|
extensions: []
|
52
52
|
extra_rdoc_files: []
|
53
53
|
files:
|
54
|
+
- ".gitignore"
|
55
|
+
- Gemfile
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- __tests__/app_pan_baidu_1.rb
|
59
|
+
- __tests__/app_pan_baidu_2.rb
|
60
|
+
- __tests__/app_pan_baidu_3.rb
|
61
|
+
- __tests__/app_upload.rb
|
62
|
+
- bin/console
|
63
|
+
- bin/setup
|
64
|
+
- example/upload_weibo.rb
|
65
|
+
- example/upload_weibo_back.rb
|
66
|
+
- example/upload_weibo_http.rb
|
54
67
|
- lib/nx-http.rb
|
68
|
+
- lib/nx/content_type.rb
|
69
|
+
- lib/nx/data_transform.rb
|
70
|
+
- lib/nx/http.rb
|
71
|
+
- lib/nx/version.rb
|
72
|
+
- nx-http-0.1.1.gem
|
73
|
+
- nx-http-0.1.2.gem
|
74
|
+
- nx-http.gemspec
|
75
|
+
- package.json
|
55
76
|
homepage: https://github.com/afeiship/nx-http
|
56
77
|
licenses:
|
57
78
|
- MIT
|