當前位置: 首頁>>代碼示例>>Golang>>正文


Golang http.Response類代碼示例

本文整理匯總了Golang中net/http.Response的典型用法代碼示例。如果您正苦於以下問題:Golang Response類的具體用法?Golang Response怎麽用?Golang Response使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Response類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ResponseToJso

func ResponseToJso(o *otto.Object, w *http.Response) {
	o.Set("status", w.Status)
	o.Set("header", w.Header)
	o.Set("cookies", w.Cookies())
	o.Set("statusCode", w.StatusCode)
	o.Set("proto", w.Proto)
	c, _ := ioutil.ReadAll(w.Body)
	o.Set("body", string(c))
	if w.TLS != nil {
		o.Set("tlsServerName", w.TLS.ServerName)
		o.Set("tlsNegotiatedProtocol", w.TLS.NegotiatedProtocol)
		names := []string{}
		ips := []string{}
		for _, k := range w.TLS.PeerCertificates {
			for _, n := range k.DNSNames {
				names = append(names, n)
			}
			for _, n := range k.IPAddresses {
				ips = append(ips, n.String())
			}
		}
		o.Set("tlsDNSNames", names)
		o.Set("tlsIPs", ips)
	}
}
開發者ID:BlackEspresso,項目名稱:ServeJS,代碼行數:25,代碼來源:http.go

示例2: TestOAuth2Provider_Non200Response

func TestOAuth2Provider_Non200Response(t *testing.T) {

	config := &common.Config{
		Map: objx.MSI(
			OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
			OAuth2KeyScope, OAuth2KeyScope,
			OAuth2KeyClientID, OAuth2KeyClientID,
			OAuth2KeySecret, OAuth2KeySecret,
			OAuth2KeyAuthURL, OAuth2KeyAuthURL,
			OAuth2KeyTokenURL, OAuth2KeyTokenURL)}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testProvider := new(test.TestProvider)

	testResponse := new(http.Response)
	testResponse.Header = make(http.Header)
	testResponse.Header.Set("Content-Type", "text/plain")
	testResponse.StatusCode = 401
	testResponse.Body = ioutil.NopCloser(strings.NewReader("No mate"))

	testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	data := objx.MSI(OAuth2KeyCode, []string{"123"})
	_, err := CompleteAuth(testTripperFactory, data, config, testProvider)

	if assert.Error(t, err) {
		assert.IsType(t, &common.AuthServerError{}, err)
	}

	mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)

}
開發者ID:jmptrader,項目名稱:gomniauth,代碼行數:34,代碼來源:complete_auth_test.go

示例3: handleResp

// handleResp handles the responses from the etcd server
// If status code is OK, read the http body and return it as byte array
// If status code is TemporaryRedirect, update leader.
// If status code is InternalServerError, sleep for 200ms.
func (c *Client) handleResp(resp *http.Response) (bool, []byte) {
	defer resp.Body.Close()

	code := resp.StatusCode

	if code == http.StatusTemporaryRedirect {
		u, err := resp.Location()

		if err != nil {
			logger.Warning(err)
		} else {
			c.cluster.updateLeaderFromURL(u)
		}

		return false, nil

	} else if code == http.StatusInternalServerError {
		time.Sleep(time.Millisecond * 200)

	} else if validHttpStatusCode[code] {
		b, err := ioutil.ReadAll(resp.Body)

		if err != nil {
			return false, nil
		}

		return true, b
	}

	logger.Warning("bad status code ", resp.StatusCode)
	return false, nil
}
開發者ID:kula,項目名稱:etcdctl,代碼行數:36,代碼來源:requests.go

示例4: getSessionAndAppPort

func getSessionAndAppPort(url string, rPort uint16) (*http.Cookie, *http.Cookie, string) {
	var client http.Client
	var req *http.Request
	var resp *http.Response
	var err error
	var port []byte

	uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)

	req, err = http.NewRequest("GET", uri, nil)
	Expect(err).ToNot(HaveOccurred())

	resp, err = client.Do(req)
	Expect(err).ToNot(HaveOccurred())

	port, err = ioutil.ReadAll(resp.Body)
	Expect(err).ToNot(HaveOccurred())

	var sessionCookie, vcapCookie *http.Cookie
	for _, cookie := range resp.Cookies() {
		if cookie.Name == proxy.StickyCookieKey {
			sessionCookie = cookie
		} else if cookie.Name == proxy.VcapCookieId {
			vcapCookie = cookie
		}
	}

	return sessionCookie, vcapCookie, string(port)
}
開發者ID:rakutentech,項目名稱:gorouter,代碼行數:29,代碼來源:router_test.go

示例5: TestDealWithItParentTest

func TestDealWithItParentTest(t *testing.T) {
	stub := StubHandler().(*stubHandler)
	target := ChartHandler(make(chan bool), &Results{}, stub).(*chartHandler)

	expectedData := []byte("Expected Data")
	var response http.Response
	response.Body = nopCloser{bytes.NewBuffer(expectedData)}

	testTime := time.Now()
	timer := StubTimer(testTime, testTime.Add(time.Hour))

	go func() {
		<-target.logger
	}()
	target.DealWithIt(response, &timer)

	if stub.dealCalled == 0 {
		t.Errorf("TestDealWithItParent: Failed to call DealWithIt on the parent")
	}

	result, err := ioutil.ReadAll(stub.savedBody)
	if err != nil {
		t.Errorf("TestDealWithItParent: Failed to read expected data from parent call, err %v", err)
	}
	if !bytes.Equal(result, expectedData) {
		t.Errorf("TestDealWithItParent: Expected '%v' but got '%v'", expectedData, result)
	}

}
開發者ID:gawth,項目名稱:gravo,代碼行數:29,代碼來源:charthandler_test.go

示例6: prepareResponseHeaders

func prepareResponseHeaders(res *http.Response) {
	// remove global hop-by-hop headers.
	for _, h := range hopHeaders {
		res.Header.Del(h)
	}

	// remove the Upgrade header and headers referenced in the Connection
	// header if HTTP < 1.1 or if Connection header didn't contain "upgrade":
	// https://tools.ietf.org/html/rfc7230#section-6.7
	if !res.ProtoAtLeast(1, 1) || !isConnectionUpgrade(res.Header) {
		res.Header.Del("Upgrade")

		// A proxy or gateway MUST parse a received Connection header field before a
		// message is forwarded and, for each connection-option in this field, remove
		// any header field(s) from the message with the same name as the
		// connection-option, and then remove the Connection header field itself (or
		// replace it with the intermediary's own connection options for the
		// forwarded message): https://tools.ietf.org/html/rfc7230#section-6.1
		tokens := strings.Split(res.Header.Get("Connection"), ",")
		for _, hdr := range tokens {
			res.Header.Del(hdr)
		}
		res.Header.Del("Connection")
	}
}
開發者ID:imjorge,項目名稱:flynn,代碼行數:25,代碼來源:reverseproxy.go

示例7: Do

// Do sends an HTTP request with the provided http.Client and returns an HTTP response.
// If the client is nil, http.DefaultClient is used.
// If the context is canceled or times out, ctx.Err() will be returned.
func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
	if client == nil {
		client = http.DefaultClient
	}

	// Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
	cancel := canceler(client, req)

	type responseAndError struct {
		resp *http.Response
		err  error
	}
	result := make(chan responseAndError, 1)

	// Make local copies of test hooks closed over by goroutines below.
	// Prevents data races in tests.
	testHookDoReturned := testHookDoReturned
	testHookDidBodyClose := testHookDidBodyClose

	go func() {
		resp, err := client.Do(req)
		testHookDoReturned()
		result <- responseAndError{resp, err}
	}()

	var resp *http.Response

	select {
	case <-ctx.Done():
		testHookContextDoneBeforeHeaders()
		cancel()
		// Clean up after the goroutine calling client.Do:
		go func() {
			if r := <-result; r.resp != nil {
				testHookDidBodyClose()
				r.resp.Body.Close()
			}
		}()
		return nil, ctx.Err()
	case r := <-result:
		var err error
		resp, err = r.resp, r.err
		if err != nil {
			return resp, err
		}
	}

	c := make(chan struct{})
	go func() {
		select {
		case <-ctx.Done():
			cancel()
		case <-c:
			// The response's Body is closed.
		}
	}()
	resp.Body = &notifyingReader{resp.Body, c}

	return resp, nil
}
開發者ID:CowLeo,項目名稱:distribution,代碼行數:63,代碼來源:ctxhttp.go

示例8: transformResponse

func (c *imageTransformer) transformResponse(r *http.Response) {

	var images []APIImages

	if err := json.NewDecoder(r.Body).Decode(&images); err != nil {
		return
	}

	for _, im := range images {

		if im.Labels == nil {
			im.Labels = make(map[string]string)
		}

		im.Labels["hola"] = "world"
	}

	var b bytes.Buffer
	w := bufio.NewWriter(&b)

	// Now take the struct and encode it
	if err := json.NewEncoder(w).Encode(&images); err != nil {
		return
	}

	// Restore the io.ReadCloser to its original state
	r.Body = ioutil.NopCloser(bytes.NewBuffer(b.Bytes()))

	// Set size of modified body
	r.ContentLength = int64(binary.Size(b))
}
開發者ID:morfeo8marc,項目名稱:docker-multi-tenancy,代碼行數:31,代碼來源:image.go

示例9: streamAndCache

// Code to stream the result in lock-step to the client and to a cache file.
// If conn terminates, continue streaming the response into the cache.
func streamAndCache(res *http.Response, conn net.Conn, cache_path string) int64 {
	response_reader, cache_writer := io.Pipe()
	defer func() {
		err := cache_writer.Close()
		if err != nil {
			panic(err)
		}
	}()

	n_recvd := make(chan int64)
	go func() {
		n_recvd <- CacheResponse(cache_path, response_reader)
	}()

	// Swallow write errors to `conn` since we want it to keep writing to the
	// cache even if conn goes away
	w := io.MultiWriter(cache_writer, writeErrorSwallower{conn})
	err := res.Write(w)
	if err != nil {
		panic(err)
	}

	// Wait until CacheResponse is done copying the response to a file.
	n := <-n_recvd
	return n
}
開發者ID:khia,項目名稱:httpcache,代碼行數:28,代碼來源:main.go

示例10: traceHttpResponse

func traceHttpResponse(c *Configuration, res *http.Response) {
	if res == nil {
		return
	}

	tracerx.Printf("HTTP: %d", res.StatusCode)

	if c.isTracingHttp == false {
		return
	}

	fmt.Fprintf(os.Stderr, "\n")
	fmt.Fprintf(os.Stderr, "< %s %s\n", res.Proto, res.Status)
	for key, _ := range res.Header {
		fmt.Fprintf(os.Stderr, "< %s: %s\n", key, res.Header.Get(key))
	}

	traceBody := false
	ctype := strings.ToLower(strings.SplitN(res.Header.Get("Content-Type"), ";", 2)[0])
	for _, tracedType := range tracedTypes {
		if strings.Contains(ctype, tracedType) {
			traceBody = true
		}
	}

	res.Body = newCountedResponse(res)
	if traceBody {
		res.Body = newTracedBody(res.Body)
	}

	fmt.Fprintf(os.Stderr, "\n")
}
開發者ID:miumok98,項目名稱:git-lfs,代碼行數:32,代碼來源:http.go

示例11: handlerWriteResponse

func (srv *Server) handlerWriteResponse(request *Request, res *http.Response, c net.Conn, bw *bufio.Writer) error {
	// Setup write stage
	request.startPipelineStage("server.ResponseWrite")
	request.CurrentStage.Type = PipelineStageTypeOverhead

	// cleanup
	defer func() {
		request.finishPipelineStage()
		request.finishRequest()
		srv.requestFinished(request, res)
		if res.Body != nil {
			res.Body.Close()
		}
	}()

	// Cycle nodelay flag on socket
	// Note: defers for FILO so this will happen before the write
	// 		 phase is complete, which is what we want.
	if nodelay := srv.setNoDelay(c, false); nodelay {
		defer srv.setNoDelay(c, true)
	}

	var err error
	// Write response
	if err = res.Write(bw); err != nil {
		return err
	}

	// Flush any remaining buffer
	err = bw.Flush()

	return err
}
開發者ID:dcmrlee,項目名稱:falcore,代碼行數:33,代碼來源:server.go

示例12: SendForTraversal

func (session *Session) SendForTraversal(url string, data string) (string, error) {
	var (
		resp *http.Response // http response
		buf  bytes.Buffer   // contains http response body
		err  error
	)
	resp, err = session.request(url, data)
	if err != nil {
		return "", err
	}
	log.Println(resp)
	defer func() {
		if resp.Body != nil {
			resp.Body.Close()
		}
	}()
	_, err = buf.ReadFrom(resp.Body)
	if err != nil {
		return "", err
	}
	session.StatusCode = resp.StatusCode
	location, err := resp.Location()
	if err != nil {
		return "", err
	}
	session.Location = location.String()
	return buf.String(), nil
}
開發者ID:cloudcube,項目名稱:goneo4j,代碼行數:28,代碼來源:session.go

示例13: RoundTrip

func (lrt *LoggingRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
	var err error
	var res *http.Response

	log.Printf("Forwarding to: %s\n", request.URL.String())
	res, err = lrt.transport.RoundTrip(request)
	if err != nil {
		return nil, err
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatalln(err.Error())
	}
	log.Println("")
	log.Printf("Response Headers: %#v\n", res.Header)
	log.Println("")
	log.Printf("Response Body: %s\n", string(body))
	log.Println("")
	res.Body = ioutil.NopCloser(bytes.NewBuffer(body))

	log.Println("Sending response to GoRouter...")

	return res, err
}
開發者ID:asifhuawei,項目名稱:logging-route-service,代碼行數:25,代碼來源:main.go

示例14: Download

func (self *Surfer) Download(sp *spider.Spider, cReq *request.Request) *spider.Context {
	ctx := spider.GetContext(sp, cReq)

	var resp *http.Response
	var err error

	switch cReq.GetDownloaderID() {
	case SURF_ID:
		resp, err = self.surf.Download(cReq)

	case PHANTOM_ID:
		resp, err = self.phantom.Download(cReq)
	}

	if resp.StatusCode >= 400 {
		err = errors.New("響應狀態 " + resp.Status)
	}

	if resp.Header.Get("Content-Encoding") == "gzip" {
		var gzipReader *gzip.Reader
		gzipReader, err = gzip.NewReader(resp.Body)
		resp.Body.Close()
		if err == nil {
			resp.Body = ioutil.NopCloser(gzipReader)
		}
	}

	ctx.SetResponse(resp).SetError(err)

	return ctx
}
開發者ID:ReinhardHsu,項目名稱:pholcus,代碼行數:31,代碼來源:downloader_surfer.go

示例15: downloadFile

//下載文件,並對字符編碼做相應的處理
func (this *HttpDownLoader) downloadFile(p *page.Page, req *page.Request) (*page.Page, string) {
	var err error
	var httpResp *http.Response
	var urlStr string
	var method string
	urlStr = req.GetUrl()
	if len(urlStr) == 0 {
		logs.GetFirstLogger().Error("url is empty")
		p.SetStatus(true, "url is empty")
		return p, ""
	}

	method = req.GetMethod()

	if method == "POST" {
		httpResp, err = http.Post(req.GetUrl(), "application/x-www-form-urlencoded", strings.NewReader(req.GetPostData()))
	} else {
		httpResp, err = http.Get(req.GetUrl())
	}

	if err != nil {
		logs.GetFirstLogger().Error("http visit error :" + err.Error())
		p.SetStatus(true, err.Error())
	}
	p.SetHeader(httpResp.Header)
	p.SetCookies(httpResp.Cookies())
	body, _ := ioutil.ReadAll(httpResp.Body)
	bodyStr := string(body)
	defer httpResp.Body.Close()
	return p, bodyStr
}
開發者ID:dulumao,項目名稱:game_spider,代碼行數:32,代碼來源:downloader_http.go


注:本文中的net/http.Response類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。