如何从浏览器请求Rails中的JSON响应

时间:2018-02-28 20:07:37

标签: ruby-on-rails

我想仅使用浏览器测试来自Rails控制器GET操作的JSON响应。

如果资源的网址是 SSLContextImpl: Using X509ExtendedKeyManager com.ibm.jsse2.hd SSLContextImpl: Using X509TrustManager com.ibm.jsse2.pc JsseJCE: Using SecureRandom IBMSecureRandom from provider IBMJCE version 1.2 trigger seeding of SecureRandom done seeding SecureRandom instantiated an instance of class com.ibm.jsse2.SSLSocketFactoryImpl IBMJSSE2 will not enable CBC protection IBMJSSE2 to send SCSV Cipher Suite on initial ClientHello JsseJCE: Using SecureRandom IBMSecureRandom from provider IBMJCE version 1.2 JsseJCE: Using cipher AES/CBC/NoPadding from provider TBD via init CipherBox: Using cipher AES/CBC/NoPadding from provider from init IBMJCE version 1.2 IBMJSSE2 will allow RFC 5746 renegotiation per com.ibm.jsse2.renegotiate set to none or default IBMJSSE2 will not require renegotiation indicator during initial handshake per com.ibm.jsse2.renegotiation.indicator set to OPTIONAL or default taken IBMJSSE2 will not perform identity checking against the peer cert check during renegotiation per com.ibm.jsse2.renegotiation.peer.cert.check set to OFF or default Is initial handshake: true %% No cached client session *** ClientHello, TLSv1.2 RandomCookie: GMT: 1503070341 bytes = { 152, 50, 18, 78, 108, 96, 63, 98, 44, 14, 255, 58, 89, 161, 90, 194, 150, 17, 22, 60, 58, 30, 156, 194, 83, 148, 201, 11 } Session ID: {} Cipher Suites: [SSL_RSA_WITH_RC4_128_MD5, SSL_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_AES_128_CBC_SHA, SSL_RSA_WITH_AES_256_CBC_SHA, SSL_DHE_RSA_WITH_AES_128_CBC_SHA, SSL_DHE_RSA_WITH_AES_256_CBC_SHA, SSL_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_DHE_DSS_WITH_AES_256_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_RC4_128_SHA, SSL_RSA_WITH_DES_CBC_SHA, SSL_RSA_FIPS_WITH_DES_CBC_SHA, SSL_DHE_RSA_WITH_DES_CBC_SHA, SSL_DHE_DSS_WITH_DES_CBC_SHA, SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA, SSL_RENEGO_PROTECTION_REQUEST] Compression Methods: { 0 } Extension signature_algorithms, signature_algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA224withECDSA, SHA224withRSA, SHA1withECDSA, SHA1withRSA, SHA256withDSA, SHA1withDSA, MD5withRSA *** main, WRITE: TLSv1.2 Handshake, length = 121 main, READ: TLSv1.2 Alert, length = 2 main, RECV TLSv1 ALERT: fatal, handshake_failure main, called closeSocket() main, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure at com.ibm.jsse2.o.a(o.java:8) at com.ibm.jsse2.o.a(o.java:4) at com.ibm.jsse2.SSLSocketImpl.b(SSLSocketImpl.java:40) at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:554) at com.ibm.jsse2.SSLSocketImpl.h(SSLSocketImpl.java:223) at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:724) at com.ibm.jsse2.SSLSocketImpl.startHandshake(SSLSocketImpl.java:81) at com.ibm.net.ssl.www2.protocol.https.c.afterConnect(c.java:8) at com.ibm.net.ssl.www2.protocol.https.d.connect(d.java:20) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1207) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:390) at com.ibm.net.ssl.www2.protocol.https.b.getResponseCode(b.java:36) at Java6withHostHeader.main(Java6withHostHeader.java:94) ,如何告诉rails将响应作为JSON返回?

2 个答案:

答案 0 :(得分:0)

一旦你弄清楚如何去做,这很简单。

您可以通过将类型添加为操作的扩展名来指定响应的类型。

例如,如果我们想要json响应,可以将请求更改为http://localhost:3000/models/action.json

答案 1 :(得分:0)

您必须在控制器中定义响应格式:

def action 
   @objects = Model.all
   respond_to do |format|
      format.html
      format.json
   end
end

或作为shorcut

def action 
   @objects = Model.all
   respond_to :html, :json
end

OR处理控制器级别的格式

class ModelsController < ApplicationController
   respond_to :html, :json

   def action
     @objects = Model.all
     respond_with(@objects)
  end
end

现在,当您呼叫http://localhost:3000/models/action.json时,响应将为JSON格式